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;
}
I see:
http://screenshotuploader.com/s/01/IeUSR6YuL
Any idea what I have done wrong?
In response to the earlier comment about IE8 becoming less prominent, I'm afraid that won't happen as soon as we might like. Many people are stubbornly happy on their Windows XP systems and for good reason, it was a pretty decent OS.
Microsoft has made the decision for some reason or other to not support XP in IE9 regardless of how many people are still running it and the fact that other programs such as FireFox can support current standards for all current OS's including XP. It is explained pretty well in http://download.cnet.com/8301-2007_4-20043103-12.html .
As long as XP users cannot use IE9 we will have to continue to offer backwards compatibility for some time to come. I expext IE8 to be the next generations thorn in the side like IE6 proved to be as long as IE9 cannot run on XP.
Just my opinion.
BTW, nice snippet Hawkee, I'm about to release a similar one but mine isn't styled nearly as nice.
Although its bad, I usually kick off all my IE users and tell them to feckoff and get a better browser. Fun to do even though its pretty much removing alot of users.
IE9 seems to finally start to catch up with todays standards hopefully this is the start of browsers working together to make a standard which is actually a standard.
Thank you, but unfortunately the rounded corners don't work on IE8. I'm hoping IE9 becomes widely adopted quickly so we can put this whole lack of functionality in IE behind us.
And yes I'm happy to see the influx of PHP code. After reviewing my statistics I noticed my PHP code has the highest number of pageviews by far.
Nice! I usually find pagination to be one of those things I end up rewriting for every project because A) I lose it B) I cant be bothered to find it in my other files same with logins. I might pop up my user class on here for easy user creation on sites of which I keep for all my projects. Nice to see a small influx(Oxymoron?) of PHP scripts ^^
I also like the styling, sleek and modern (in my eyes)!
(I commented earlier but forgot to click post >.> so now it looks like im only commenting on yours because you commented on mine xD)