网站地图    收藏   

主页 > php专栏 > php分页 >

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

来源:自学PHP网    时间:2014-11-30 14:41 作者: 阅读:

[导读] ?php Adam 39;sCustomPHPMySQLPaginationTutorialandScript YouhavetoputyourmysqlconnectiondataandaltertheSQLqueries(bothqueries)...

一个简单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. // This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query 
  51. $limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;  
  52. // Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax 
  53. // $sql2 is what we will use to fuel our while loop statement below 
  54. $sql2 = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC $limit");  
  55. //////////////////////////////// END Adam's Pagination Logic //////////////////////////////////////////////////////////////////////////////// 
  56. ///////////////////////////////////// Adam's Pagination Display Setup ///////////////////////////////////////////////////////////////////// 
  57. $paginationDisplay = ""// Initialize the pagination output variable 
  58. // 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 
  59. if ($lastPage != "1"){ 
  60.     // This shows the user what page they are on, and the total number of pages 
  61.     $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage'&nbsp;  &nbsp;  &nbsp; '
  62.     // If we are not on page 1 we can place the Back button 
  63.     if ($pn != 1) { 
  64.         $previous = $pn - 1; 
  65.         $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> '
  66.     }  
  67.     // Lay in the clickable numbers display here between the Back and Next links 
  68.     $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>'
  69.     // If we are not on the very last page we can place the Next button 
  70.     if ($pn != $lastPage) { 
  71.         $nextPage = $pn + 1; 
  72.         $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> '
  73.     }  
  74. ///////////////////////////////////// END Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////////// 
  75. // Build the Output Section Here 
  76. $outputList = ''
  77. while($row = mysql_fetch_array($sql2)){ 
  78.     $id = $row["id"]; 
  79.     $firstname = $row["firstname"]; 
  80.     $country = $row["country"]; 
  81.     $outputList .= '<h1>' . $firstname . '</h1><h2>' . $country . ' </h2><hr />'
  82.      
  83. // close while loop 
  84. ?> 
  85. <html> 
  86. <head> 
  87. <title>Adam's Pagination</title> 
  88. <style type="text/css"
  89. <!-- 
  90. .pagNumActive { 
  91.     color: #000; 
  92.     border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px; 
  93. .paginationNumbers a:link { 
  94.     color: #000; 
  95.     text-decoration: none; 
  96.     border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px; 
  97. .paginationNumbers a:visited { 
  98.     color: #000; 
  99.     text-decoration: none; 
  100.     border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px; 
  101. .paginationNumbers a:hover { 
  102.     color: #000; 
  103.     text-decoration: none; 
  104.     border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px; 
  105. .paginationNumbers a:active { 
  106.     color: #000; 
  107.     text-decoration: none; 
  108.     border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px; 
  109. --> 
  110. </style> 
  111. </head> 
  112. <body> 
  113.    <div style="margin-left:64px; margin-right:64px;"
  114.      <h2>Total Items: <?php echo $nr; ?></h2> 
  115.    </div>  
  116.       <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div> 
  117.       <div style="margin-left:64px; margin-right:64px;"><?php print "$outputList"; ?></div> 
  118.       <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div> 
  119. </body> 
  120. </html> 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论