Sistema di paginazione con MySQL e PHP

Qui sotto trovate un sistema di paginazione con MySQL e PHP. Tra i tanti nella rete questo lo reputo fatto bene, a voi la scelta!

  1.  
  2.  
  3. < ?php
  4.  
  5. $pages=$_GET['page']; //Get the current page
  6.  
  7.  
  8. //Connect to your MySQL
  9.  
  10.  
  11. $mysql_link = mysql_connect("localhost", "namef", "pass");
  12. mysql_select_db("test") or die("Could not select database");
  13.  
  14. // This the full code which will generate a paging system
  15. $limit=10; // Limit of result per page
  16.  
  17. $qresult = mysql_query("SELECT * FROM sample_db"); // Let's get the query
  18.  
  19. $nrResults=mysql_num_rows($qresult); // Count the results
  20.  
  21. if (($nrResults%$limit)<>0) {
  22. $pmax=floor($nrResults/$limit)+1; // Divide to total result by the number of query you want
  23. // to display per page($limit) and create a Max page
  24. } else {
  25. $pmax=floor($nrResults/$limit);
  26. }
  27. $qresult = mysql_query("SELECT * FROM sample_d DESC LIMIT ".(($_GET["page"]-1)*$limit).", $limit");
  28. //Need to generate query considering your limit
  29. while($line = mysql_fetch_array( $qresult ))
  30.  
  31. {
  32. // Now once we got the query from MySQL, we need to think what we to do it. You can do anything. This is just an example
  33.  
  34. Name: ".$line['name']."
  35. Age: ".$line['age']."
  36. Phone Number: ".$line['phone']."
  37.  
  38. ";
  39.  
  40. // Here comes the Real part of this Tutorial!!
  41.  
  42. } echo "<div class='navpage'>"; // Make a simple css
  43.  
  44. // We need to create a Previous page, so we need $pages to be bigger than 1, otherwise at the first page we would get a 0.
  45. //For the previous to show up we need at least to be at the second page.
  46. if($pages > 1) {
  47. $prevp="<a href='http://mysite.com/page.php?page=".($pages-1)."' title='Previous Page'>Previous Page</a>"; } else {echo "";}
  48. echo $prevp;
  49.  
  50. // Here We want create a page from the results we got by dividing the total by the the limit. So let say you got
  51. //45 results and you want 5 results per page; these simple lines will create you 8 pages.
  52.  
  53. $pid=1;
  54. while ($pid< =$pmax) {
  55. $paging= "<a href='http://mysite.com/page.php?page=$pid' title='Page $pid of $pmax'> $pid";
  56.  
  57. //So here, let say we are at the 3rd page and we want the 3 to be blank so the user can know where is he now.
  58. //This will act as Current Page! We need to replace the url by a text
  59.  
  60. $newpaging=str_replace("<a href='http://mysite.com/page.php?page=$pages' title='Page $pages of $pmax'> $pages</a>", "<span>$pages</span>", $paging);
  61. echo $newpaging;
  62.  
  63. $pid++; // create pages until reach the result
  64.  
  65.  
  66. }
  67. //We want to create a next page and a last page, $pages have to be less than $pmax.
  68. if($pages < $pmax) {
  69. $nextp="<a href='http://mysite.com/page.php?page=".($pages+1)."' title='Next Page'>Next Page"; } else {echo "";}
  70. echo $nextp;
  71. echo "<a href='http://mysite.com/page.php?page=$pmax' title='Last Page'>Last Page</a>";
  72. echo "</div>";
  73.  
  74. ?>
  75.