How to create a digg and sabros.us style pagination with PHP
I made a few modifications to the sabros.us’s pagination script, inspired by the original created by strangerstudios.com, it needs a detail, turn it into a dynamic and re-usable function without so much roll.
Now the form to use it is super simple, we just call the function pagination() with the next parameters:
- Amount of pages
- Items by page
- Present page (or the one that is being seen)
- The page to which it will send the parameter of the following one (previous, first, last) page(?page=X)
- The amount of adjacent links to the present page
One of the new special things (not very flexible yet) he facility to use the url_friendly.
The page parameter (fourth parameter) we can send it like text and it will take it literally, adding the parameter to the end of the page of the following way default.php?page=1.
echo pagination(1000,10,5,"index.php");
but if we sent the page on form of array on the following form: array(”http://localhost/index/page/[…].html“,”[…]“,1)
echo pagination(1000,10,5,array("http://localhost/index/page/[...]","[...]"));
this will replace the last parameter (the present page) with the data between […], allowing us therefore the facility to use url_Friendly quickly :-).
Clarifying that I did not analyze all the necessities of the script, it was improvised by my necessity to use it in several systems ;-), so… it is possible that it helps to somebody
<?php
/*
Plugin Name: *Digg Style Paginator
Plugin URI: http://www.mis-algoritmos.com/2006/11/23/paginacion-al-estilo-digg-y-sabrosus/
Description: Adds a <strong>digg style pagination</strong>.
Version: 0.1 Beta
*/
function pagination($total_pages,$limit,$page=1,$file="paginator.php",$adjacents=2){
#$total_pages; //total number of rows in data table
#$limit; //how many items to show per page
#$page = isset($_GET['page'])?$_GET['page']:1;
#$file = "paginator.php";
#$file = array("digg-[...].html","[...]");
#$adjacents = 3;
/* Setup vars for query. */
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //anterior page is page - 1
$siguiente = $page + 1; //siguiente page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$url_friendly = false;
if(is_array($file))
$url_friendly=true;
$p = false;
if(strpos($file,"?")>0)
$p = true;
ob_start();
if($lastpage > 1){
echo "<div class=\"pagination\">";
//anterior button
if($page > 1)
if($url_friendly)
echo "<a href=\"".str_replace($file[1],$prev,$file[0])."\">« Anterior</a>";
else
if($p)
echo "<a href=\"$file&page=$prev\">« Anterior</a>";
else
echo "<a href=\"$file?page=$prev\">« Anterior</a>";
else
echo "<span class=\"disabled\">« Anterior</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)){//not enough pages to bother breaking it up
for ($counter = 1; $counter <= $lastpage; $counter++){
if ($counter == $page)
echo "<span class=\"current\">$counter</span>";
else
if($url_friendly)
echo "<a href=\"".str_replace($file[1],$counter,$file[0])."\">$counter</a>";
else
if($p)
echo "<a href=\"$file&page=$counter\">$counter</a>";
else
echo "<a href=\"$file?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)){//enough pages to hide some
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2)){
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
if ($counter == $page)
echo "<span class=\"current\">$counter</span>";
else
if($url_friendly)
echo "<a href=\"".str_replace($file[1],$counter,$file[0])."\">$counter</a>";
else
if($p)
echo "<a href=\"$file&page=$counter\">$counter</a>";
else
echo "<a href=\"$file?page=$counter\">$counter</a>";
}
echo "...";
if($url_friendly){
echo "<a href=\"".str_replace($file[1],$lpm1,$file[0])."\">$lpm1</a>";
echo "<a href=\"".str_replace($file[1],$lastpage,$file[0])."\">$lastpage</a>";
}else{
if($p){
echo "<a href=\"$file&page=$lpm1\">$lpm1</a>";
echo "<a href=\"$file&page=$lastpage\">$lastpage</a>";
}else{
echo "<a href=\"$file?page=$lpm1\">$lpm1</a>";
echo "<a href=\"$file?page=$lastpage\">$lastpage</a>";
}
}
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)){
if($url_friendly){
echo "<a href=\"".str_replace($file[1],1,$file[0])."\">1</a>";
echo "<a href=\"".str_replace($file[1],2,$file[0])."\">2</a>";
}else{
if($p){
echo "<a href=\"$file&page=1\">1</a>";
echo "<a href=\"$file&page=2\">2</a>";
}else{
echo "<a href=\"$file?page=1\">1</a>";
echo "<a href=\"$file?page=2\">2</a>";
}
}
echo "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
if ($counter == $page)
echo "<span class=\"current\">$counter</span>";
else
if($url_friendly)
echo "<a href=\"".str_replace($file[1],$counter,$file[0])."\">$counter</a>";
else
if($p)
echo "<a href=\"$file&page=$counter\">$counter</a>";
else
echo "<a href=\"$file?page=$counter\">$counter</a>";
echo "...";
if($url_friendly){
echo "<a href=\"".str_replace($file[1],$lpm1,$file[0])."\">$lpm1</a>";
echo "<a href=\"".str_replace($file[1],$lastpage,$file[0])."\">$lastpage</a>";
}else{
if($p){
echo "<a href=\"$file&page=$lpm1\">$lpm1</a>";
echo "<a href=\"$file&page=$lastpage\">$lastpage</a>";
}else{
echo "<a href=\"$file?page=$lpm1\">$lpm1</a>";
echo "<a href=\"$file?page=$lastpage\">$lastpage</a>";
}
}
}
//close to end; only hide early pages
else{
if($url_friendly){
echo "<a href=\"".str_replace($file[1],1,$file[0])."\">1</a>";
echo "<a href=\"".str_replace($file[1],2,$file[0])."\">2</a>";
}else{
if($p){
echo "<a href=\"$file&page=1\">1</a>";
echo "<a href=\"$file&page=2\">2</a>";
}else{
echo "<a href=\"$file?page=1\">1</a>";
echo "<a href=\"$file?page=2\">2</a>";
}
}
echo "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
if ($counter == $page)
echo "<span class=\"current\">$counter</span>";
else
if($url_friendly)
echo "<a href=\"".str_replace($file[1],$counter,$file[0])."\">$counter</a>";
else
if($p)
echo "<a href=\"$file&page=$counter\">$counter</a>";
else
echo "<a href=\"$file?page=$counter\">$counter</a>";
}
}
//siguiente button
if ($page < $counter - 1)
if($url_friendly)
echo "<a href=\"".str_replace($file[1],$siguiente,$file[0])."\">Siguiente »</a>";
else
if($p)
echo "<a href=\"$file&page=$siguiente\">Siguiente »</a>";
else
echo "<a href=\"$file?page=$siguiente\">Siguiente »</a>";
else
echo "<span class=\"disabled\">Siguiente »</span>";
echo "</div>\n";
}
return utf8_decode(ob_get_clean());
}
?>
And before I forget it, for the appearance of the Digg’s pagination, just add in the style sheet the following lines (or you can select a style from this page):
[css]
div.pagination {
padding: 3px;
margin: 3px;
}
div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #AAAADD;
text-decoration: none; /* no underline */
color: #000099;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;
color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #000099;
font-weight: bold;
background-color: #000099;
color: #FFF;
}
div.pagination span.disabled {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #EEE;
color: #DDD;
}
[/css] 7 Comments
Make A CommentComments RSS Feed TrackBack URL




November 28th, 2006 at 6:32 pm
(pardon my poor spanish)
Bueno! Esto es muy similares a cómo lo hice con WineLog.
November 29th, 2006 at 12:24 pm
Thank you for the code Jason Coleman
January 12th, 2007 at 9:12 pm
Found a bug in your code, lines 85-87 (when left and right adjacents will be showing):
else
$pagina_uf = is_array($pagina)?str_replace($pagina[1],$counter,$pagina[0]):$pagina.”?page=$counter”;
$pagination.= “$counter“;
should be
else {
$pagina_uf = is_array($pagina)?str_replace($pagina[1],$counter,$pagina[0]):$pagina.”?page=$counter”;
$pagination.= “$counter“;
}
otherwise a duplicate link will appear. Example, if you have 20 pages and you click on page 5, next to the 5 will be another 5 that links to page 4.
January 14th, 2007 at 11:18 pm
Gracias Ted, lo repararéFixedMarch 12th, 2007 at 7:14 pm
[...] creación de esta plugin de paginación fue basada en la versión modular creada a partir de la original creada por [...]
March 16th, 2007 at 5:16 pm
[...] estilos aplican para la versión modular del paginador, y la realizada por [...]
May 27th, 2007 at 7:27 pm
[...] on the modular version for the pagination, now I have created a version easier to implement, using classes for PHP version [...]