Drupal Pagination with a Custom Query

Pagination is a commonly used UI concept anytime you plan to display lots of content to your audience. We're all pretty familiar with doing this through Views UI, but I ran across a quirk in the Drupal API when trying to create a pager for a custom query.

The functions pager_query and theme_pager automagically work hand-in-hand using global variables.

Fredric Mitchell
#Drupal | Posted

Pagination is a commonly used UI concept anytime you plan to display lots of content to your audience. We're all pretty familiar with doing this through Views UI, but I ran across a quirk in the Drupal API when trying to create a pager for a custom query.

The functions pager_query and theme_pager automagically work hand-in-hand using global variables.

For one of our recent projects, we built the commenting system as a custom content type with multiple node reference fields. While writing a Views handler was an option, a custom pager allowed us to quickly optimize the user experience.

// Function to get comments via pager_query
function

myexample_custom_comments($node_nid) {
$sql = "SELECT n.nid FROM {node} n
LEFT JOIN {content_type_mycontent_type} mc
ON mc.nid = n.nid
WHERE mc.field_custom_nid = %d
AND n.status = 1
ORDER BY n.created DESC"
;
$result = pager_query($sql, 10, 0, NULL, array($node_nid));
while($records = db_result($result)) {
$comments[] = $records;
}

return $comments;
}

// Custom function to theme above results

function theme_myexample_custom_comments_pager() {
return theme('pager');
}

The third parameter in pager_query is an ID that allows for multiple pagers on the same page, as demonstrated on the NYC G.D.O.

If you want a custom pager, but don't plan to do a database call (perhaps you're manipulating third-party data), you'll need to manipulate the global variables directly via preprocess in your template.

$items_per_page = 10;
//add this to the variable array so we might output it in the template$variables['items_per_page']=$items_per_page;//what page you are on$GLOBALS['pager_page_array'][]=array_key_exists('page',$variables['params'])

? check_plain($_GET['page']) : 0;

// total number of pages
$GLOBALS['pager_total'][] = ceil($results->totalmatches / $items_per_page);

$variables['pager'] = theme('pager', NULL, $items_per_page);

Pretty neat huh?

The only hangup is that pager_query is not in D7. Extenders, however, are a new concept that should be explored.

Fredric Mitchell