Home  >  Article  >  php教程  >  Reprint: Paging principle + paging code + paging class production

Reprint: Paging principle + paging code + paging class production

WBOY
WBOYOriginal
2016-09-24 09:02:46760browse

Paging display is a very common method of browsing and displaying large amounts of data, and it is one of the most commonly processed events in web programming. For veterans of web programming, writing this kind of code is as natural as breathing, but for beginners, they are often confused about this issue, so I specially wrote this article to explain this issue in detail.

1. Paging principle:
The so-called paging display is to artificially divide the result set in the database into sections for display. Two initial parameters are required here:

How many records per page ($PageSize)?
What page is the current page ($CurrentPageID)?

Now just give me another result set, and I can display a specific result.

As for other parameters, such as: previous page ($PReviousPageID), next page ($NextPageID), total number of pages ($numPages), etc., they can be obtained based on the previous things.

Taking the MySQL database as an example, if you want to intercept a certain piece of content from the table, the sql statement can be used: select * from table limit offset, rows. Take a look at the following set of SQL statements and try to find the rules.

The first 10 records: select * from table limit 0,10
The 11th to 20th records: select * from table limit 10,10
The 21st to 30th records: select * from table limit 20,10
……语 This group of SQL statements is actually when $ PageSize = 10, take the SQL statement of each page of the table in the table. We can summarize such a template:
Select * from Table Limit ($ CurrentPageid -1) * $ PageSize $ PAGESIZE a Take this template into the corresponding value and compare the set of SQL statements above to see if it is. After solving the most important problem of how to obtain the data, all that is left is to pass the parameters, construct the appropriate SQL statement and then use PHP to obtain the data from the database and display it.




2. Pagination code description: five steps The code is fully explained and can be copied to your own notepad for direct use

  1. Employee information list
  2.                                 //Display information of all emp tables
  3.                        //1. Connect to the database
  4.             $conn=mysql_connect('localhost','root','1234abcd') or die('Connect to database error'.mysql_error());
  5. 库 // 2. Select the database 2
  6. mysql_select_db('empManage');
  7.                                //3. Select character set
  8. mysql_query('set names utf8');
  9.                                //4. Send sql statement and get the result for processing
  10. 页 //4.1 Pagling [Pagling to emit two SQL statements, one is to get $ Rowcount, and the other is to obtain the paging result through the Limit of SQL. So we will get two result sets. Remember to distinguish them when naming them.
  11. Paging (four values ​​and two sql statements). ]
  12.                                                                                                                                                                                                                                                                                                      
  13.                                       $rowCount=0;//How many records are there in total
  14. O $ pagenow = 1; // hope to display the page of the first page
  15. C $ PageCount = 0; // How many pages are there? Since $rowCount can be obtained from the server, the initial value can be given as 0;
  16. $pageNow indicates which page you want to display. It is best to set it to 0 here; $pageSize indicates how many records are displayed on each page. This is determined in advance according to the needs of the website.
  17. $pageCount=ceil($rowCount/$pageSize), since $rowCount can have an initial value of 0, then $pageCount can of course be set to 0. Four indicators, two 0, one 1, and the other is the website requirement. ]
  18.                                                                                                                         ’ ’ ’ to 4.15
  19. ’ s - 1 t- it to be paging link
  20.                        if(!empty($_GET['pageNow'])){
  21.                                  $pageNow=$_GET['pageNow'];
  22.                   }[Modify the value of $pageNow based on the paging link. ]
  23.              $sql='select count(id) from emp';
  24.                                                   $res1=mysql_query($sql);
  25.                                                                                                 using   using   using                 through ’’s ’ through ’ s through cue through through ’ through ’ through ’ through ’ through ’ through ’ through'' way through through out's' back through through‐‐to‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐‐‐‐‐‐
  26.             if($row=mysql_fetch_row($res1)){
  27.                                                                                                                                     $rowCount=$row[0];
  28.          }//[Get $rowCount, and once we enter, we will know the two indicators $pageCount. ]
  29.                        //4.12 Calculate how many pages there are
  30.            $pageCount=ceil($rowCount/$pageSize);
  31.             $pageStart=($pageNow-1)*$pageSize;
  32.                           //4.13 Send sql results with pagination
  33. = $ SQL = "Select * from Emp Limit $ PAGESTATART, $ PAGESIZE"; // [Based on the two values ​​(starting values, number per page) behind the limit of the $ SQL statement, to achieve paging. and find these two values.]
  34.         $res2=mysql_query($sql,$conn) or die('无法获取结果集'.mysql_error());
  35.         echo '';[        echo "
    ";]
  36. "
  37. ";        while($row=mysql_fetch_assoc($res2)){
  38.                 echo "
  39. ";        }
  40.         echo '
  41. idnamegradeemailsalary删除用户修改用户
    {$row['id']}{$row['name']}{$row['grade']}{$row['email']}{$row['salary']}删除用户修改用户
    ';
  42.         //4.14打印出页码的超链接
  43.         for($i=1;$i<=$pageCount;$i++){
  44.                 echo "$i ";//[打印出页码的超链接]
  45.         
  46.         }
  47.         //5.释放资源,关闭连接
  48.         mysql_free_result($res2);
  49.         mysql_close($conn);
  50. ?>


三、简单分页类分享

         现在公布一个简单分类制作。只要理解了这个类的原理和步骤,其他复杂类也就能够触类旁通了。不废话,直接上源码,可以直接用在你的项目中。

        数据库操作类代码:mysqli.func.php
      

  1. // 数据库连接常量  
  2. define('DB_HOST', 'localhost');  
  3. define('DB_USER', 'root');  
  4. define('DB_PWD', '');  
  5. define('DB_NAME', 'guest');  
  6.   
  7. // 连接数据库  
  8. function conn()  
  9. {  
  10.     $conn = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME);  
  11.     mysqli_query($conn, "set names utf8");  
  12.     return $conn;  
  13. }  
  14.   
  15. //获得结果集  
  16. function doresult($sql){  
  17.    $result=mysqli_query(conn(), $sql);  
  18.    return  $result;  
  19. }  
  20.   
  21. //结果集转为对象集合  
  22. function dolists($result){  
  23.     return mysqli_fetch_array($result, MYSQL_ASSOC);  
  24. }  
  25.   
  26. function totalnums($sql) {  
  27.     $result=mysqli_query(conn(), $sql);  
  28.     return $result->num_rows;  
  29. }  
  30.   
  31. // 关闭数据库  
  32. function closedb()  
  33. {  
  34.     if (! mysqli_close()) {  
  35.         exit('关闭异常');  
  36.     }  
  37. }  
  38.   
  39. ?>   


Paging implementation code:


  1. include 'mysqli.func.php';
  2. //Total number of records
  3. $sql = "SELECT dg_id FROM tb_user ";
  4. $totalnums = totalnums($sql);
  5. //Number of items displayed on each page
  6. $fnum = 8;
  7. //Number of pages turned
  8. $pagenum = ceil($totalnums / $fnum);
  9. // Page number constant
  10. @$tmp = $_GET['page'];
  11. //Prevent malicious page turning
  12. if ($tmp > $pagenum)
  13. echo "";
  14. //Calculate the starting value of paging
  15. if ($tmp == "") {
  16. $num = 0;
  17. } else {
  18. $num = ($tmp - 1) * $fnum;
  19. }
  20. // Query statement
  21. $sql = "SELECT dg_id,dg_username FROM tb_user ORDER BY dg_id DESC LIMIT " . $num . ",$fnum";
  22. $result = doresult($sql);
  23. // Traverse output
  24. while (! ! $rows = dolists($result)) {
  25. echo $rows['dg_id'] . " " . $rows['dg_username'] . "
    ";
  26. }
  27. // Page turning link
  28. for ($i = 0; $i < $pagenum; $i ++) {
  29. echo "" . ($i + 1) . "";
  30. }
  31. ?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn