Pagination Function - Rounded Gradient Buttons
Platform: PHP
Published Mar 21, 2011
Updated Feb 11, 2013
This function will return a list of page numbers similar to the Google search results style. It will currently show up to 15 page numbers at a time and this value can be adjusted within the function.
Your code will need to handle the $_REQUEST['p'] value itself.
HTML example:
<div class='pages'>
<?php print display_pages($total_pages, $current_page, $items_per_page, $page_url); ?>
</div>You may use this stylesheet to match the screenshot:
.pages {
text-align: center;
width: 646px;
padding-bottom: 20px;
padding: 2px 8px;
border: solid 1px #CCC;
color: #000;
border-radius: 8px;
-moz-border-radius: 8px;
background: -moz-linear-gradient(top, #FFF, #EAEAEA);
background: -webkit-gradient(linear, left top, left bottom, from(#FFF), to(#EAEAEA));
filter: progid:DXImageTransform.Microsoft.Gradient(StartColorStr='#FFF', EndColorStr='#EAEAEA', GradientType=0);
}
.pages a:hover, .pages .current {
color: #FFF;
background: #999;
border: 1px solid #999;
}
/************************************************************************************/
// Takes in the number of items, the current page and the number of items per page
// to build a list of links. The $page_url needs to be sent from the calling script.
function display_pages($items, $page, $show, $page_url) {
if($page > 1) {
$previous_page = $page - 1;
$previous_url = "$page_url&p=$previous_page";
// Make sure the first parameter begins with a ? and not a &
$previous_url = preg_replace("/\/\&/", '/?', $previous_url);
$previous = "<a href='$previous_url'><b>Prev</b></a> ";
}
$s = ($page + 1) * $show;
if($s < $items) {
$next_page = $page + 1;
$next_url = "$page_url&p=$next_page";
// Make sure the first parameter begins with a ? and not a &
$next_url = preg_replace("/\/\&/", '/?', $next_url);
$next = "<a href='$next_url'><b>Next</b></a>";
}
$total_pages = $items / $show;
// Number of page links to show.
if(($page + 1) == $total_pages) {
$page_numbers = 15;
}
else if($page > 0) {
if($page < 7) {
$page_numbers = 15 - $page;
}
else {
$page_numbers = 7;
}
}
else {
$page_numbers = 15;
}
for($page_inc = $page - $page_numbers; $page_inc <= $page + $page_numbers; $page_inc++) {
$print_num = $page_inc + 1;
if($page_inc >= 0 and $page_inc < $total_pages) {
if($page_inc != $page) {
$page_link = "$page_url&p=$page_inc";
$page_link = preg_replace("/\/\&/", '/?', $page_link);
$page_links .= "<a href='$page_link'>$print_num</a> ";
}
else {
$page_links .= "<span class='current'>$print_num</span> ";
}
}
}
if($next or $previous) {
if($next and $previous) {
$link = $previous.$page_links.$next;
}
else if($next) {
$link = $page_links.$next;
}
else if($previous) {
$link = $previous.$page_links;
}
}
return $link;
}