Thursday, October 6, 2011

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

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() in includes/pager.inc I managed to get it to work. Maybe someone else will find this useful.
Just remember to change "themename" (in the function name) to the name of your theme, otherwise the override won't work.
<?php/**
*Override  themename_pager()
* 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($tags = array(), $limit = 10, $element = 0, $parameters = array()) {

    global
$pager_page_array, $pager_total, $pager_total_items;
   
$page_prev = $pager_page_array[$element] - 1;
   
$page_curr = $pager_page_array[$element] + 1;
   
$page_next = $pager_page_array[$element] + 1;
   
$page_last = $pager_total[$element] - 1;

  
   
//Custom Label
   
$this_pg = $pager_page_array[$element]+1;
   
$last_pg = $pager_total[$element];  
   
$total = $pager_total_items[0];
   
$start = $page_curr*10 - 9;
    if(
$this_pg < $last_pg)
   
$end = $this_pg*10 ;
    else
   
$end = $total;

   
$label = "Comments $start-$end of $total";
   
//end of custom label



   
if ($pager_total[$element] > 1) {
 
       
$output = '<div id="pagination" class="pager">';

        if (
$pager_page_array[$element]!=0) {
         
           
$output.= '<span class="pager-first pager-first-active"><a href="?page=0">First</a></span>';
         
           
$output.= '<span class="pager-previous pager-previous-active"><a href="?page='.$page_prev.'">Prev</a></span>';
     
        } else {
     
           
$output.= '<span class="pager-first pager-inactive pager-first-inactive">First</span>';
         
           
$output.= '<span class="pager-previous pager-inactive pager-previous-inactive">Prev</span>';

        }

       
$output.= '<span class="pager-pagenumbers">'. $label .'</span>';


        if (
$page_curr!=$pager_total[$element]) {
     
           
$output.= '<span class="pager-next pager-next-active"><a href="?page='.$page_next.'">Next</a></span>';
     
           
$output.= '<span class="pager-last pager-last-active"><a href="?page='.$pager_total[$element].'">Last</a></span>';
 
        } else {
     
           
$output.= '<span class="pager-next pager-inactive pager-next-inactive">Next</span>';

           
$output.= '<span class="pager-last pager-inactive pager-last-inactive">Last</span>';
        }

       
$output.= '</div>';

        return
$output;
    }
}
?>

No comments:

Post a Comment