| 5 pager.inc | theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array()) |
| 6 pager.inc | theme_pager( |
| 7 pager.inc | theme_pager($variables) |
| 8 pager.inc | theme_pager($variables) |
Returns HTML for a query pager.
Menu callbacks that display paged query results should call theme('pager') to retrieve a pager control so that users can view other results. Format a list of nearby pages with additional query results.
Parameters
$tags: An array of labels for the controls in the pager.
$limit: The number of query results to display per page.
$element: An optional integer to distinguish between multiple pagers on one page.
$parameters: An associative array of query string parameters to append to the pager links.
$quantity: The number of pages in the list.
Return value
An HTML string that generates the query pager.
Related topics
File
- includes/
pager.inc, line 114 - Functions to aid in presenting database results as a set of pages.
Code
<?php
function theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9) {
global $pager_page_array, $pager_total;
// 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.
$li_first = theme('pager_first', (isset($tags[0]) ? $tags[0] : t('« first')), $limit, $element, $parameters);
$li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('‹ previous')), $limit, $element, 1, $parameters);
$li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('next ›')), $limit, $element, 1, $parameters);
$li_last = theme('pager_last', (isset($tags[4]) ? $tags[4] : t('last »')), $limit, $element, $parameters);
if ($pager_total[$element] > 1) {
if ($li_first) {
$items[] = array(
'class' => 'pager-first',
'data' => $li_first,
);
}
if ($li_previous) {
$items[] = array(
'class' => 'pager-previous',
'data' => $li_previous,
);
}
// When there is more than one page, create the pager list.
if ($i != $pager_max) {
if ($i > 1) {
$items[] = array(
'class' => 'pager-ellipsis',
'data' => '…',
);
}
// Now generate the actual pager piece.
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
if ($i < $pager_current) {
$items[] = array(
'class' => 'pager-item',
'data' => theme('pager_previous', $i, $limit, $element, ($pager_current - $i), $parameters),
);
}
if ($i == $pager_current) {
$items[] = array(
'class' => 'pager-current',
'data' => $i,
);
}
if ($i > $pager_current) {
$items[] = array(
'class' => 'pager-item',
'data' => theme('pager_next', $i, $limit, $element, ($i - $pager_current), $parameters),
);
}
}
if ($i < $pager_max) {
$items[] = array(
'class' => 'pager-ellipsis',
'data' => '…',
);
}
}
// End generation.
if ($li_next) {
$items[] = array(
'class' => 'pager-next',
'data' => $li_next,
);
}
if ($li_last) {
$items[] = array(
'class' => 'pager-last',
'data' => $li_last,
);
}
return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
}
}
?> Login or register to post comments
Comments
DeviantArt style pager
If you want a pager in the style of the Deviantart site were 'first'&'last' links are hidden and the ellipsis are attached to the prev/next page numbers like so:
< prev ...3 4 5 6 7... next >
This put this code in your theme template.php file and rename the function to match your own theme name.
function yourthemename_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 5) {
global $pager_page_array, $pager_total;
$tags = array("", "< prev", "", "next >", "");
// 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.
$li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('‹ previous')), $limit, $element, 1, $parameters);
$li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('next ›')), $limit, $element, 1, $parameters);
if ($pager_total[$element] > 1) {
if ($li_previous) {
$items[] = array(
'class' => 'pager-previous',
'data' => $li_previous,
);
}
// When there is more than one page, create the pager list.
if ($i != $pager_max) {
// Now generate the actual pager piece.
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
if ($i < $pager_current) {
if ($pager_first > 1 && $i == $pager_first) {
$output = '...'.$i;
$stopPreEllipsis = true;
} else {
$output = $i;
}
$items[] = array(
'class' => 'pager-item',
'data' => theme('pager_previous', $output, $limit, $element, ($pager_current - $i), $parameters),
);
}
if ($i == $pager_current) {
$items[] = array(
'class' => 'pager-current',
'data' => $i,
);
}
if ($i > $pager_current) {
if ($pager_last < $pager_max && $i == $pager_last) {
$output = $i.'...';
} else {
$output = $i;
}
$items[] = array(
'class' => 'pager-item',
'data' => theme('pager_next', $output, $limit, $element, ($i - $pager_current), $parameters),
);
}
}
}
// End generation.
if ($li_next) {
$items[] = array(
'class' => 'pager-next',
'data' => $li_next,
);
}
return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
}
}
error on the last pages
When the pager gets close to the last page, the marker "..." is added incorrectly:
«prev 5 ...6 7 [8] 9
Error
Hi,
while i am adding this code in my basic theme template.I am getting following error.
Cannot unset string offsets in D:\Install\xampp\htdocs\myproject\sites\all\modules\devel_themer\devel_themer.module
how can i fix this error ?
A small bug
I think you should set $pager_first back to $i after
// 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;
//should add
$pager_first = $i;
That may cause "..." being added incorrectly when comes to the last pages of the pager
iStock photos style
For an iStock photo style of pager, put this code in your template.php file:
It looks similar to
« ‹ … 3 | 4 | 5 | 6 | 7 of 21 › »
function yourthemename_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 5) {
global $pager_page_array, $pager_total;
// 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.
$pager_delim = ' |';
// 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.
$li_first = theme('pager_first', (isset($tags[0]) ? $tags[0] : t('« ')), $limit, $element, $parameters);
$li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('â¹ ')), $limit, $element, 1, $parameters);
$li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('âº')), $limit, $element, 1, $parameters);
$li_last = theme('pager_last', (isset($tags[4]) ? $tags[4] : t('»')), $limit, $element, $parameters);
if ($pager_total[$element] > 1) {
if ($li_first) {
$items[] = array(
'class' => 'pager-first',
'data' => $li_first,
);
}
if ($li_previous) {
$items[] = array(
'class' => 'pager-previous',
'data' => $li_previous,
);
}
// When there is more than one page, create the pager list.
if ($i != $pager_max) {
if ($i > 1) {
$items[] = array(
'class' => 'pager-ellipsis',
'data' => 'â¦',
);
}
// Now generate the actual pager piece.
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
if ($i < $pager_current) {
$items[] = array(
'class' => 'pager-item',
'data' => theme('pager_previous', $i, $limit, $element, ($pager_current - $i), $parameters).$pager_delim,
);
}
if ($i == $pager_current) {
$items[] = array(
'class' => 'pager-current',
/*'data' => '['.$i.']'.$pager_delim,*/
'data' => $i,
);
}
if ($i > $pager_current) {
$items[] = array(
'class' => 'pager-item',
'data' => $pager_delim.theme('pager_next', $i, $limit, $element, ($i - $pager_current), $parameters),
);
}
}
if ($i < $pager_max) {
$items[] = array(
'class' => 'pager-ellipsis',
'data' => 'of '.$pager_max,
);
}
}
// End generation.
if ($li_next) {
$items[] = array(
'class' => 'pager-next',
'data' => $li_next,
);
}
if ($li_last) {
$items[] = array(
'class' => 'pager-last',
'data' => $li_last,
);
}
return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
}
}
I used the following CSS as well:
/* Make pagers right-justified */
div.item-list .pager {
text-align: right;
}
div.item-list ul.pager li {
margin: 0px;
font-size : 0.8em;
padding:2px;
}
div.item-list ul.pager li.pager-first,
div.item-list ul.pager li.pager-previous,
div.item-list ul.pager li.pager-next,
div.item-list ul.pager li.pager-last {
background-image:url('images/pager_box.png');
background-position: 0px 4px;
padding:0.5em;
}
div.item-list ul.pager li.pager-first:hover,
div.item-list ul.pager li.pager-previous:hover,
div.item-list ul.pager li.pager-next:hover,
div.item-list ul.pager li.pager-last:hover {
background-image:url('images/pager_box.png');
background-position: 0px -16px;
padding:0.5em;
}
pager_box.png files are just boxes that fit around the « and ‹ . Make an image 15 x 35 with two vertically stacked boxes: a 15x15 empty box at 0,0 and a 15x15 shaded box at 0,20.
Not able to get it work
Hi,
Thank you for this code.
I have copied this code in my template.php, but somehow in place of links and arrows for next/prev/page numbers it is printing the text 'Array'. I am using Drupal 6.
Can you please let me know what could be the cause.
Thank you,
Jay Mehta.
Line91 should be less or equal
for line 91 change
if ($i < $pager_max) {
to
if ($i <= $pager_max) {
Showing 1-50 of 53 next 3 ›
To have a pager in the following format.
<?php
function themename_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9){
global $pager_page_array, $pager_total, $pager_total_items;
$page = $pager_page_array[0] + 1;
$first_no = $limit * $pager_page_array[0] + 1;
$last_no = ($limit * $page) > $pager_total_items[0] ? $pager_total_items[0] : $limit * $page;
$next_no = $pager_total_items[0] - ($page * $limit) > $limit ? 50 : $pager_total_items[0] - ($page * $limit);
$previous_no = $limit;
$li_text = 'Showing '.$first_no.'-'.$last_no.' of '. $pager_total_items[0];
$li_previous = theme('pager_previous', t('‹ previous @no',array('@no'=>$previous_no)), $limit, $element, 1, $parameters);
$li_next = theme('pager_next', t('next @no ›',array('@no'=>$next_no)), $limit, $element, 1, $parameters);
if ($li_previous) {
$items[] = array(
'class' => 'pager-previous',
'data' => $li_previous,
);
}
if ($li_text) {
$items[] = array(
'class' => 'pager-text',
'data' => $li_text,
);
}
if ($li_next) {
$items[] = array(
'class' => 'pager-next',
'data' => $li_next,
);
}
return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
}
?>
Some advancement
You hardcoded the number 50 in this line:
$next_no = $pager_total_items[0] - ($page * $limit) > $limit ? 50 : $pager_total_items[0] - ($page * $limit);This makes no sense for me, instead i'll suggest the following:
$next_no = $pager_total_items[0] - ($page * $limit) > $limit ? $limit : $pager_total_items[0] - ($page * $limit);So the function displays not a hardcoded number, though the limit or if less than the limit the lasting ones.
quick correction
$pager_total_items[0] should be $pager_total_items[$element]
I have taken your wonderful
I have taken your wonderful pager a step further and added a "<< first" and "last >>", which my team wanted. Here it is:
<?php
/**
* Implementation of HOOK_pager().
*/
function [themename]_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9){
global $pager_page_array, $pager_total, $pager_total_items;
$page = $pager_page_array[0] + 1;
$first_no = $limit * $pager_page_array[0] + 1;
$last_no = ($limit * $page) > $pager_total_items[0] ? $pager_total_items[0] : $limit * $page;
$next_no = $pager_total_items[0] - ($page * $limit) > $limit ? $limit : $pager_total_items[0] - ($page * $limit);
$previous_no = $limit;
$li_first = theme('pager_first', t('‹‹ first',array('@no'=>$previous_no)), $limit, $element, $parameters);
$li_text = 'Showing '.$first_no.'-'.$last_no.' of '. $pager_total_items[0];
$li_previous = theme('pager_previous', t('‹ previous @no',array('@no'=>$previous_no)), $limit, $element, 1, $parameters);
$li_next = theme('pager_next', t('next @no ›',array('@no'=>$next_no)), $limit, $element, 1, $parameters);
$li_last = theme('pager_last', t('last ››',array('@no'=>$pager_total)), $limit, $element, $parameters);
if ($li_first) {
$items[] = array(
'class' => 'pager-first',
'data' => $li_first,
);
}
if ($li_previous) {
$items[] = array(
'class' => 'pager-previous',
'data' => $li_previous,
);
}
if ($li_text) {
$items[] = array(
'class' => 'pager-text',
'data' => $li_text,
);
}
if ($li_next) {
$items[] = array(
'class' => 'pager-next',
'data' => $li_next,
);
}
if ($li_last) {
$items[] = array(
'class' => 'pager-last',
'data' => $li_last,
);
}
return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
}
?>
Edit: You may also need the code below if you are using ApacheSolr on your site. I corrected the text in the pager. The core Search module had set a variable that we no longer use, and the variable value was 10 when we currently are using 20. The variable is now dynamic, so if we choose to show more search results per page then the pager text will reflect that. I have the ApacheSolr running on my site.
<?php/**
* Process variables for search-results.tpl.php.
*
* The $variables array contains the following arguments:
* - $results
* - $type
*
* @see search-results.tpl.php
*/
function [themename]_preprocess_search_results(&$variables) {
$variables['pager'] = theme('pager', NULL, variable_get('apachesolr_rows', 20), 0);
}
?>
Your Own Pager w/ Custom Query
For D6:
To use theme_pager with your own custom query, you must use pager_query as it sets globals that theme_pager will use.
// Function to get comments via pager_query
function _myexample_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_comments_pager() {
return theme('pager');
}
Or, set those globals
Or, set those globals yourself:
<?php
global $pager_page_array, $pager_total;
$page = isset($_GET['page']) ? check_plain($_GET['page']) : 0;
$pager_page_array = explode(',', $page);
$pager_total = array(ceil($num_results / $items_per_page));
?>
Wordpress-like Previous/Next only
If you want it to behave just like most Wordpress pagers, showing only previous and next links, this short theme_pager override will do. It is a cleaned up version of theme_pager:
<?php
function yourtheme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9) {
global $pager_total;
$li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('‹ previous')), $limit, $element, 1, $parameters);
$li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('next ›')), $limit, $element, 1, $parameters);
if ($pager_total[$element] > 1) {
if ($li_previous) {
$items[] = array(
'class' => 'pager-previous',
'data' => $li_previous,
);
}
// End generation.
if ($li_next) {
$items[] = array(
'class' => 'pager-next',
'data' => $li_next,
);
}
return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
}
}
?>
An advanced userfriendly pager in D6
To make an easy-to-use pager, put this in your template.php, and you will get something like:
Make sure to change YOURTHEME to correct name.
<?php
/**
* Implementation of theme_pager().
*/
function YOURTHEME_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 5) {
global $pager_page_array, $pager_total;
$tags = array("« first", "‹ previous", "", "next ›", "last »");
// 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;
$pager_first = $i;
}
if ($i <= 0) {
// Adjust "center" if at start of query.
$pager_last = $pager_last + (1 - $i);
$i = 1;
}
// End of generation loop preparation.
$li_first = theme('pager_first', $tags[0], $limit, $element, $parameters);
$li_previous = theme('pager_previous', $tags[1], $limit, $element, 1, $parameters);
$li_next = theme('pager_next', $tags[3], $limit, $element, 1, $parameters);
$li_last = theme('pager_last', $tags[4], $limit, $element, $parameters);
if ($pager_total[$element] > 1) {
// Add pager first link
if ($li_first) {
$items[] = array(
'class' => 'pager-first',
'data' => $li_first,
);
}
// Add pager previous link
if ($li_previous) {
$items[] = array(
'class' => 'pager-previous',
'data' => $li_previous,
);
}
// When there is more than one page, create the pager list.
// Middle items start
if ($i != $pager_max) {
// Now generate the actual pager piece.
for (; $i <= $pager_last && $i <= $pager_max; $i++) {
if ($i < $pager_current) {
if ($pager_first > 1 && $i == $pager_first) {
// Add pager breaker
$items[] = array(
'class' => 'pager-breaker',
'data' => '...',
);
}
$items[] = array(
'class' => 'pager-item',
'data' => theme('pager_previous', $i, $limit, $element, ($pager_current - $i), $parameters),
);
}
if ($i == $pager_current) {
$items[] = array(
'class' => 'pager-current',
'data' => $i,
);
}
if ($i > $pager_current) {
$items[] = array(
'class' => 'pager-item',
'data' => theme('pager_next', $i, $limit, $element, ($i - $pager_current), $parameters),
);
if ($pager_last < $pager_max && $i == $pager_last) {
// Add pager breaker
$items[] = array(
'class' => 'pager-breaker',
'data' => '...',
);
$items[] = array(
'class' => 'pager-item',
'data' => theme('pager_last', $pager_max, $limit, $element, $parameters),
);
}
}
}
}
// Middle items end
// Add next pager link
if ($li_next) {
$items[] = array(
'class' => 'pager-next',
'data' => $li_next,
);
}
// Add last pager link
if ($li_last) {
$items[] = array(
'class' => 'pager-last',
'data' => $li_last,
);
}
return theme('item_list', $items, NULL, 'ul', array('class' => 'mdPager'));
}
}
?>
Allways show prev and next?
Hello, how can I force to allways show the PREV / NEXT buttons? Maybe with a non clickable element?
Thank's
just check if $li_previous or
just check if $li_previous or $li_next is blank and set with simple text.
// Setting if previous page exists
$li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('‹ Prev Page')), $limit, $element, 1, $parameters);
// Check if any theme input recieved, otherwise set with simple text to show
if (!$li_previous) {
$li_previous = t('‹ Prev Page');
}
Same is to done with $li_next
// Setting if next page exists
$li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('Next Page ›')), $limit, $element, 1, $parameters);
// Check if any theme input recieved, otherwise set with simple text to show
if (!$li_next) {
$li_next = t('Next Page ›');
}
Best of luck..
Facebook Like pager
hello I wonder how to make facebook like pager
where instead of will be [more] and
with ajax query method get or post it will get the limit number
i tried to
function theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9) {
global $pager_page_array, $pager_total;
if(isset($_GET['news_limit']) && is_int($_GET['news_limit'])) {
$limit = $_GET['news_limit'];
}else{
$limit = 10;
but it's not working , help please