Simple PHP & MySQL Pagination

Page last updated on 2011 / 04 / 09

When looking at MySQL output, it is sometimes more convenient to split up the number of records returned into separate pages, and include hyperlinks to further pages in the result set; a layout often referred to as pagination.

The following is an example of such pagination. Change the MySQL query in the example at the foot of the code to see it working for yourself, remembering to connect to your MySQL database beforehand.

  1. <?php
  2.  
  3. function mysql_query_pagination($query_string,$records_per_page)
  4. {
  5. $return = array(2=>'');
  6.  
  7. /* Uses $_GET['page'] to determine which page number you are viewing */
  8. $baseuri = preg_replace(array("'(&|?)page=d+'"),'',$_SERVER['REQUEST_URI']);
  9. if(strpos($baseuri,'?'))
  10. { $baseuri .= '&'; }
  11. else
  12. { $baseuri .= '?'; }
  13.  
  14. /* If the page is not the first one, generate navigation to link to previous pages */
  15. if(isset($_GET['page']) && ctype_digit($_GET['page']) && $_GET['page'] > 0)
  16. {
  17. $page = $_GET['page'];
  18. $return[2] .= '<a href="'.$baseuri.'page=0">First Page</a>.. ';
  19. $a = 0;
  20. for($i = ($page > 10 ? $page-10 : 1);$i < $page;$i++)
  21. { $return[2] .= '<a href="'.$baseuri.'page='.$i.'">'.$i.'</a> '; }
  22. }
  23. else
  24. { $page = 0; } $return[2] .= ($page > 0 ? '<b>'.$page.'</b>' : 'First Page').' ';
  25.  
  26. /* Get total number of rows to find out how many links to other pages should be generated */
  27. $all_rows = mysql_query(preg_replace("'SELECT(.+)FROM'ims",'SELECT COUNT(*) FROM',$query_string))
  28. or die(mysql_error());
  29. $all_rows = mysql_fetch_array($all_rows,MYSQL_ASSOC);
  30. /* Perform actual query sent to function */
  31. $return[3] = mysql_query($query_string.' LIMIT '.($page*$records_per_page).','.$records_per_page)
  32. or die(mysql_error());
  33. $return[0] = $all_rows['COUNT(*)'];
  34. if(!($return[1] = mysql_num_rows($return[3])))
  35. { RETURN FALSE; }
  36.  
  37. $return[4] = ($page * $records_per_page) + 1;
  38. $i = 0;
  39. $return[4] += $records_per_page;
  40. $x = $page;
  41. while($return[4] <= $return[0] && $i < 10)
  42. {
  43. ++$x;
  44. $return[2] .= '<a href="'.$baseuri.'page='.$x.'">'.$x.'</a> ';
  45. $return[4] += $records_per_page;
  46. $i++;
  47. }
  48.  
  49. if($return[4] < $return[0]-1)
  50. { $return[2] .= '..<a href="'.$baseuri.'page='.(floor($return[0]/$return[1])).'">Last Page</a> ' }; unset($return[4]);
  51. $return[2] = '<div align="center" id="mysql_nav" class="mysql_nav">
  52. Navigation (Results #'.(($page * $records_per_page) + 1).' to #'.(($page * $records_per_page) + $return[1]).'
  53. of '.$return[0].'):
  54. <br />'.$return[2].'</div>';
  55. RETURN $return;
  56. }
  57.  
  58. /*
  59. Example usage
  60.   $mysql_data[0] returns total row count
  61.   $mysql_data[1] returns number of rows returned after pagination
  62.   $mysql_data[2] returns navigation
  63.   $mysql_data[3] returns the query resource to iterate through
  64. */
  65.  
  66. // 20 rows per page
  67. if($mysql_data = mysql_query_pagination('SELECT * FROM table',20))
  68. {
  69. // Show navigation at top of results
  70. echo $mysql_data[2];
  71. echo '<pre>';
  72. while($row = mysql_fetch_array($mysql_data[3],MYSQL_ASSOC))
  73. {
  74. // Your row data ... do stuff here
  75. echo implode("t",$row)."\n";
  76. }
  77. echo '</pre>';
  78. // Show navigation at bottom of results
  79. echo $mysql_data[2];
  80. }
  81. else
  82. {
  83. echo 'No results returned';
  84. }
  85.  
  86. ?>
  87.  

Previous Article
Forking with PHP from the Command Line
Next Article
A Quick and Efficient URL Shortener Script, Using PHP & MySQL




Tweet