How to create a digg and sabros.us style pagination with PHP

Inicio ¿Sidebar? RSS @MisAlgoritmos at Twitter

How to create a digg and sabros.us style pagination with PHP

I think that this entry is obsolete. If you are looking for a pagination control, you can read this article which is eaiser to implement.

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 :D

<?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):
  1. div.pagination {
  2.    padding: 3px;
  3.    margin: 3px;
  4. }
  5.  
  6. div.pagination a {
  7.    padding: 2px 5px 2px 5px;
  8.    margin: 2px;
  9.    border: 1px solid #AAAADD;
  10.    
  11.    text-decoration: none; /* no underline */
  12.    color: #000099;
  13. }
  14. div.pagination a:hover, div.pagination a:active {
  15.    border: 1px solid #000099;
  16.  
  17.    color: #000;
  18. }
  19. div.pagination span.current {
  20.    padding: 2px 5px 2px 5px;
  21.    margin: 2px;
  22.       border: 1px solid #000099;
  23.      
  24.       font-weight: bold;
  25.       background-color: #000099;
  26.       color: #FFF;
  27.    }
  28. div.pagination span.disabled {
  29.       padding: 2px 5px 2px 5px;
  30.       margin: 2px;
  31.       border: 1px solid #EEE;
  32.    
  33.       color: #DDD;
  34.    }
There are 7 comments. for this entry.