一个简单php和mysql数据分页程序

  1. <?php
  2. // Adam's Custom PHP MySQL Pagination Tutorial and Script
  3. // You have to put your mysql connection data and alter the SQL queries(both queries)
  4. // This script is in tutorial form and is accompanied by the following video:
  5. mysql_connect("DB_Host_Here","DB_Username_Here","DB_Password_Here") or die (mysql_error());
  6. mysql_select_db("DB_Name_Here") or die (mysql_error());
  7. ////////////// QUERY THE MEMBER DATA INITIALLY LIKE YOU NORMALLY WOULD
  8. $sql = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC");
  9. //////////////////////////////////// Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////
  10. $nr = mysql_num_rows($sql); // Get total of Num rows from the database query
  11. if (isset($_GET['pn'])) { // Get pn from URL vars if it is present
  12. $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new)
  13. //$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated)
  14. } else { // If the pn URL variable is not present force it to be value of page number 1
  15. $pn = 1;
  16. }
  17. //This is where we set how many database items to show on each page
  18. $itemsPerPage = 10;
  19. // Get the value of the last page in the pagination result set
  20. $lastPage = ceil($nr / $itemsPerPage);
  21. // Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
  22. if ($pn < 1) { // If it is less than 1
  23. $pn = 1; // force if to be 1
  24. } else if ($pn > $lastPage) { // if it is greater than $lastpage
  25. $pn = $lastPage; // force it to be $lastpage's value
  26. }
  27. // This creates the numbers to click in between the next and back buttons
  28. // This section is explained well in the video that accompanies this script
  29. $centerPages = "";
  30. $sub1 = $pn - 1;
  31. $sub2 = $pn - 2;
  32. $add1 = $pn + 1;
  33. $add2 = $pn + 2;
  34. if ($pn == 1) {
  35. $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
  36. $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
  37. } else if ($pn == $lastPage) {
  38. $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
  39. $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
  40. } else if ($pn > 2 && $pn < ($lastPage - 1)) {
  41. $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
  42. $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
  43. $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
  44. $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
  45. $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
  46. } else if ($pn > 1 && $pn < $lastPage) {
  47. $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
  48. $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
  49. $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
  50. }
  51. // This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
  52. $limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
  53. // Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
  54. // $sql2 is what we will use to fuel our while loop statement below
  55. $sql2 = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC $limit");
  56. //////////////////////////////// END Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////////////
  57. ///////////////////////////////////// Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////
  58. $paginationDisplay = ""; // Initialize the pagination output variable
  59. // This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
  60. if ($lastPage != "1"){
  61. // This shows the user what page they are on, and the total number of pages
  62. $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp; &nbsp; &nbsp; ';
  63. // If we are not on page 1 we can place the Back button
  64. if ($pn != 1) {
  65. $previous = $pn - 1;
  66. $paginationDisplay .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
  67. }
  68. // Lay in the clickable numbers display here between the Back and Next links
  69. $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
  70. // If we are not on the very last page we can place the Next button
  71. if ($pn != $lastPage) {
  72. $nextPage = $pn + 1;
  73. $paginationDisplay .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
  74. }
  75. }
  76. ///////////////////////////////////// END Adam's Pagination Display Setup ///////////////////////////////////////////////////////////////////////////
  77. // Build the Output Section Here
  78. $outputList = '';
  79. while($row = mysql_fetch_array($sql2)){
  80. $id = $row["id"];
  81. $firstname = $row["firstname"];
  82. $country = $row["country"];
  83. $outputList .= '<h1>' . $firstname . '</h1><h2>' . $country . ' </h2><hr />';
  84. } // close while loop
  85. ?>
  86. <html>
  87. <head>
  88. <title>Adam's Pagination</title>
  89. <style type="text/css">
  90. <!--
  91. .pagNumActive {
  92. color: #000;
  93. border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
  94. }
  95. .paginationNumbers a:link {
  96. color: #000;
  97. text-decoration: none;
  98. border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
  99. }
  100. .paginationNumbers a:visited {
  101. color: #000;
  102. text-decoration: none;
  103. border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
  104. }
  105. .paginationNumbers a:hover {
  106. color: #000;
  107. text-decoration: none;
  108. border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
  109. }
  110. .paginationNumbers a:active {
  111. color: #000;
  112. text-decoration: none;
  113. border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
  114. }
  115. -->
  116. </style>
  117. </head>
  118. <body>
  119. <div >
  120. <h2>Total Items: <?php echo $nr; ?></h2>
  121. </div>
  122. <div ><?php echo $paginationDisplay; ?></div>
  123. <div ><?php print "$outputList"; ?></div>
  124. <div ><?php echo $paginationDisplay; ?></div>
  125. </body>
  126. </html>