Advertisement
touhid_xml

Pagination with PHP/MYSQL

Feb 8th, 2013
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // per page record
  4. $per_page = 10;
  5.  
  6. // current page
  7. if(!isset($_GET['page'])){
  8. $page = 1;
  9. }
  10. else{
  11. $page = (int) $_GET['page'];
  12. }
  13.  
  14. // mysql start position
  15. if($page<=1){
  16.  
  17.     $start = 0;
  18. }
  19. else{
  20.  
  21.     $start = $page * $per_page - $per_page;
  22. }
  23.  
  24.  
  25. //mysql connnect
  26. $con = mysql_connect('localhost','root','');
  27. mysql_select_db('page', $con);
  28.  
  29. //main sql query
  30. $sql = "SELECT * FROM pagination";
  31.  
  32. // how much record in  table
  33. $num_rows = mysql_num_rows(mysql_query($sql));
  34.  
  35. // how many pages are at all
  36. $num_pages = (int) ceil($num_rows / $per_page);
  37.  
  38. // appends query
  39. $sql .= " LIMIT $start, $per_page";
  40.  
  41. // show all records
  42. $result = mysql_query($sql);
  43.  
  44. while($row  = mysql_fetch_array($result)){
  45. print $row['name']."<br />\n";
  46. }
  47.  
  48. //prev, numbers next
  49. $prev = $page - 1;
  50. $next = $page + 1;
  51.  
  52. //pagnation
  53. print "<hr />";
  54.  
  55. //previous link
  56. if($prev>0){
  57. print "<a href=\"?page=$prev\">Previous </a>";
  58. }
  59.  
  60. //page numbers
  61. $number = 1;
  62. while($number<=$num_pages){
  63.     if($page==$number){
  64.  
  65.         print "<b>[$number]</b>";
  66.    
  67.     }
  68.         else
  69. print "<a href=\"?page=$number\">$number </a>";
  70.  
  71. $number++;
  72.  
  73. }
  74.  
  75. //next line
  76. if($page<ceil($num_rows / $per_page)){
  77. print "<a href=\"?page=$next\">Next</a>";
  78.  
  79. }
  80.  
  81. mysql_close($con);
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement