D7 Pager Query - Too many pages in the pager
I have 3 nodes of content type 'mycontenttype'. I'm trying to setup a sortable/pagable table with a limit of 10 items per page.
In this code, $nids only returns 3 nodes. I'm actually runnng node_load_multiple($nids) then looping through those nodes to build the $rows variable. Only 3 appear.
Problem: The pager is rendering with 4 pages.
Expectation: There should be no pager rendering because I do not have 10 nodes in the query or the count query.
Any insight would be greatly appreciated.
<?php
function mymodule_create_a_pager_table() {
$query = db_select('node', 'n')->extend('PagerDefault')->extend('TableSort')->element('my_custom_id');
$query->fields('n', array('nid'));
$query->condition('n.type', 'mycontenttype');
$query->condition('n.status', 1);
$count_query = clone $query;
$query->setCountQuery($count_query);
$query->limit(10);
$header = array(array('data' => 'Title', 'sort' => 'asc', 'field' => 'n.title'), 'column 2', 'column 3');
$query->orderByHeader($header);
$nids = $query->execute()->fetchCol();
// ... building $rows array for display only here
$output = theme('table', array('header'=> $header, 'rows' => $rows));
$output .= theme('pager', array('element' => 'my_custom_id', 'quantity' => 10));
return $output;
}
?>
Output
Node 1 Title | col2 val | col3 val
Node 2 Title | col2 v开发者_如何学Pythonal | col3 val
Node 3 Title | col2 val | col3 val
1 2 3 4 next › last »
I think element should be an integer.
Try to leave both the ->element() call and any arguments to theme('pager') away.
Also, your count query is wrong. Just don't define it either, that will be done automatically for you. This is probably the actual reason for your problem, not the element thing.
The count query get's executed and the first returned value (fetchField()) is assumed to be the number of elements. Your query probably returns a nid and that is mistaken as the number. So just leave that away, and Drupal will build a correct count query automatically for you.
精彩评论