Saturday, December 18, 2010

Simple Pagination

This is a simple pagination snippet I wrote for a Drupal module I was working on. I've unDruaplized it and now it's completely independent.

The function will calculate the number of pages to display based on $total and $shown. In addition, it'll append the corresponding page number to the provided $url.

NOTE: All page numbers are incremented once. This means, the function assumes URL 0 as page 1.


= 5) ? ($page - 3) : 1 );
$range_end = ( (($page + 5) > $pages ) ? $pages : ($page + 5) );

if ( $page >= 1 ) {
$r[] = '« first';
$r[] = '‹ previous';
$r[] = ( ($range_start > 1) ? ' ... ' : '' );
}

if ( $range_end > 1 ) {
foreach(range($range_start, $range_end) as $key => $value) {
if ( $value == ($page + 1) ) $r[] = ''. $value .'';
else $r[] = ''. $value .'';
}
}

if ( ( $page + 1 ) < $pages ) { $r[] = ( ($range_end < $pages) ? ' ... ' : '' ); $r[] = 'next ›';
$r[] = 'last »';
}

return ( (isset($r)) ? '
'. implode("\r\n", $r) .'
' : '');
}
?>

No comments:

Post a Comment