Customize pager to use number of results e.g. 1-10, 11-20, etc instead of page number

Last modified: October 14, 2009 - 17:29

Instead of using the usual page numbers for the pager (1, 2, 3, etc), I wanted to show the number of results i.e. 1-10, 11-20, 21-30, etc. After quite a bit of searching I finally figured out where to do the customizations and how to do it. By theming theme_pager_list() in includes/pager.inc I managed to get it to work. Maybe someone else will find this useful.

One tip that took me a while to figure out was to include the $pager_total_items global variable. You need to do that to figure out the maximum number of results.

Just remember to change "themename" (in the function name) to the name of your theme, otherwise the override won't work.

<?php
/**
* Override theme_pager_list()
* Creates a numbered list e.g. 1-10 11-20... instead of the usual page number list i.e. 1 2 3 ...
**/
function themename_pager_list($limit, $element = 0, $quantity = 5, $text = '', $parameters = array()) {
  global
$pager_page_array, $pager_total, $pager_total_items;   // **** the real trick is to include pager_total_items here

 
$output = '<span class="pager-list">';
 
// Calculate various markers within this pager piece:
  // Middle is used to "center" pages around the current page.
 
$pager_middle = ceil($quantity / 2);
 
// current is the page we are currently paged to
 
$pager_current = $pager_page_array[$element] + 1;
 
// first is the first page listed by this pager piece (re quantity)
 
$pager_first = $pager_current - $pager_middle + 1;
 
// last is the last page listed by this pager piece (re quantity)
 
$pager_last = $pager_current + $quantity - $pager_middle;
 
// max is the maximum page number
 
$pager_max = $pager_total[$element];
 
// End of marker calculations.

  // Prepare for generation loop.
 
$i = $pager_first;
  if (
$pager_last > $pager_max) {
   
// Adjust "center" if at end of query.
   
$i = $i + ($pager_max - $pager_last);
   
$pager_last = $pager_max;
  }
  if (
$i <= 0) {
   
// Adjust "center" if at start of query.
   
$pager_last = $pager_last + (1 - $i);
   
$i = 1;
  }
 
// End of generation loop preparation.

  // When there is more than one page, create the pager list.
 
if ($i != $pager_max) {
   
$output .= $text;
    if (
$i > 1) {
     
$output .= '<span class="pager-ellipsis">…</span>';
    }

   
// Now generate the actual pager piece.
   
for (; $i <= $pager_last && $i <= $pager_max; $i++) {

     
// **** create the labels for the pages here ****
     
if ($i < $pager_max) {
       
$label = ((($i - 1) * $limit) + 1) .'-'. ($i * $limit);
      }
     
// **** for the last page of results, show the range up to the highest result number e.g. 21-26 instead of 21-30 ****
     
if ($i == $pager_max) {
        if ((((
$i - 1) * $limit) + 1) == $pager_total_items[$element]) {
         
$label = ((($i - 1) * $limit) + 1);
        }
        else {
         
$label = ((($i - 1) * $limit) + 1) .'-'. $pager_total_items[$element];
        }
      }

     
// **** changed $i to $label so that the new label is used ****
     
if ($i < $pager_current) {
       
$output .= theme('pager_previous', $label, $limit, $element, ($pager_current - $i), $parameters);
      }
      if (
$i == $pager_current) {
       
$output .= '<strong class="pager-current">'. $label .'</strong>';
      }
      if (
$i > $pager_current) {
       
$output .= theme('pager_next', $label, $limit, $element, ($i - $pager_current), $parameters);
      }
    }

    if (
$i < $pager_max) {
     
$output .= '<span class="pager-ellipsis">…</span>';
    }
  }
 
$output .= '</span>';

  return
$output;
}
?>

- incrn8

The same function for drupal 6

nathan6137 - October 14, 2009 - 17:20

This function is not availible in Drupal 6. So here is a version that works with the Drupal core 6.x.

on web @ http://www.lichtman.ca
on twitter @helpontario

 
 

Drupal is a registered trademark of Dries Buytaert.