Digg-like Pagination Links
Posted: Tue Aug 12, 2008 4:51 pm
Just finished up writing this for my current project, wondered if anyone would like to improve it and/or provide criticism. I feel it's something alot of people could make use of, and can be learnt from easily because it's not included in some huge pagination class. Working example shown here (you can grab the CSS from there too). Closely follows the way Digg handle their page links.
There are some minor bugs in it, and the logic isn't great - this is where you come in
There are some minor bugs in it, and the logic isn't great - this is where you come in
Code: Select all
//Show the page links
echo '
<ul class="pagelinks">';
//Show the previous page link
if($page == 1)
echo '<li class="disabled">« Previous</li>';
else
echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($page - 1) . '">« Previous</a></li>';
//If the amount of pages is 2 or more
if($pagetotal >= 2)
//Show the first 2 page links
for($x = 1; $x <= 2; $x++)
if($page == $x)
echo '<li class="current">' . $x . '</li>';
else
echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?page=' . $x . '">' . $x . '</a></li>';
//If the page number is more than 8
if($page > 8/*SOMEONE REALL Y NEEDS TO SORT OUT "8 )" BEING TURNED INTO A SMILEY AUTOMATICALLY INSIDE CODE TAGS!!!*/)
{
//Show a spacer
echo '<li class="disabled">...</li>';
//Loop through page numbers 5 before the current page, and to a maximum of 5 after
for($x = ($page - 5); $x <= ($pagetotal - 2) && $x <= ($page + 5); $x++)
//Show the page link
if($page == $x)
echo '<li class="current">' . $x . '</li>';
else
echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?page=' . $x . '">' . $x . '</a></li>';
}
//If the total amount of pages is 3 or more
else if($pagetotal >= 3)
{
//Loop through page numbers starting from 3 and going to a maximum of 5 after the current page
for($x = 3; $x <= ($pagetotal - 2) && $x <= ($page + 5); $x++)
//Show the page link
if($page == $x)
echo '<li class="current">' . $x . '</li>';
else
echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?page=' . $x . '">' . $x . '</a></li>';
}
//If the current page is more than 7 pages from the last page
if($page < ($pagetotal - 7))
{
//Show a spacer
echo '<li class="disabled">...</li>';
}
//A special case if the page total is 3 (only one ending link is needed instead of 2)
if($pagetotal == 3)
//Show the page link
if($page == 3)
echo '<li class="current">3</li>';
else
echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?page=3">3</a></li>';
//If it's anything else over 2
else if($pagetotal > 3)
//Loop through the final 2 page numbers
for($x = ($pagetotal - 1); $x <= $pagetotal; $x++)
//Show the page link
if($page == $x)
echo '<li class="current">' . $x . '</li>';
else
echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?page=' . $x . '">' . $x . '</a></li>';
//Show the next page link
if($page == $pagetotal)
echo '<li class="disabled">Next »</li>';
else
echo '<li><a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($page + 1) . '">Next »</a></li>';
echo '
</ul>';