We have 39 guests online
PHP Scripts arrow Free PHP Tutorials arrow Free PHP Tutorials arrow PHP Tutorials and Examples arrow PHP 
Free PHP Tutorials arrow PHP Tutorials and Examples arrow PHP

Pagination as seen on Evil Walrus
User Rating: / 4
PoorBest 

This is a striped down version of the code used to generate the tabs in the search results pages. This function returns an array of the page numbers to be displayed. The array keys are ordered as they should be displayed (you could simply cycle through the array with foreach to display the page numbers). It will return a maximum of 15 page numbers, and if there are more than 15 page numbers, it will show the first and/or last 2 page numbers of the results. The array values represent the page number, and array values that are '0' represent where extra page numbers have been truncated.

  1. <?php
  2. // written by Aaron Hall, evilwalrus.org
  3. // free to copy, modify and redistribute without consent
  4.  
  5. function generatePagination($curPage, $totResults, $resultsPerPage)
  6. {
  7.     $totPages = ceil($totResults / $resultsPerPage);
  8.    
  9.     $pagesBefore = $curPage - 1;
  10.     $pagesAfter = $totPages - $curPage;
  11.    
  12.     $tabArr = array();
  13.    
  14.     if($totPages > 15) {
  15.        
  16.         if($pagesBefore > 7) {
  17.             $tabArr = array(1,2,0);
  18.            
  19.             if($pagesAfter > 7)
  20.             {
  21.                 for($i=($curPage-(4)); $i<$curPage; $i++) { $tabArr[] = $i; }
  22.             } else {
  23.                 for($i=($totPages-11); $i<$curPage; $i++) { $tabArr[] = $i; }
  24.             }
  25.         } else {
  26.             for($i=1; $i<$curPage; $i++) { $tabArr[] = $i; }
  27.         }
  28.        
  29.         $tabArr[] = $curPage;
  30.        
  31.         if($pagesAfter > 7) {
  32.             if($pagesBefore > 7) {       
  33.                 for($i=($curPage+1); $i<=$curPage+4; $i++) { $tabArr[] = $i; }
  34.             } else {
  35.                 for($i=($curPage+1); $i<13; $i++) { $tabArr[] = $i; }
  36.             }
  37.             $tabArr[] = 0;
  38.             $tabArr[] = $totPages-1;
  39.             $tabArr[] = $totPages;
  40.         } else {
  41.             for($i=($curPage+1); $i<=$totPages; $i++) { $tabArr[] = $i; }
  42.         }
  43.        
  44.     } else {
  45.         for($i=1;$i<=$totPages;$i++) { $tabArr[] = $i; }
  46.     }
  47.            
  48.     return $tabArr;
  49.    
  50. }
  51.  
  52. print_r(generatePagination(2,400,20)); // passes arguments 'current page number', 'total results', and 'results per page' respectively
  53. ?>
  54.  
  55. Returns:
  56.  
  57. Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 11 [11] => 12 [12] => 0 [13] => 19 [14] => 20 )
  58.  
  59. Here's an example usage:
  60.  
  61. <?
  62.  
  63. $currentPage = (empty($currentPage)) ? '1' : $_GET['page']; // the page the user is currently viewing -- set the $currentPage to 1 if empty, otherwise $_GET['page']
  64. $totalResults = 400; // total query results -- would probably come from mysql_num_rows on a non-limited query
  65. $listingsPerPage = 20; // how many results are being listed per page
  66.  
  67. $paginationArray = generatePagination($currentPage, $totalResults, $listingsPerPage);
  68.  
  69. foreach($paginationArray as $page) {
  70.    
  71.     if($page == 0) {
  72.         echo "..."; // print an elipse, representing pages that aren't displayed
  73.     } else {
  74.         echo "[<a href="listing.php?page=$page">$page</a>]";
  75.     }
  76.    
  77.     echo "  "; // space the pages
  78. }
  79.  
  80. ?>
  81.  
  82. returns "[1]  [2]  [3]  [4]  [5]  [6]  [7]  [8]  [9]  [10]  [11]  [12]  ...  [19]  [20]", with each as a link pointing to listing.php?page=$page

 

 

Directory Stats

There are 1036 listing and 46 categories in our website

Change Language