Php paging principle paging code paging class creation tutorial, php Paging
Paging display is a very common method for browsing and displaying large amounts of data. It is one of the most common events in web programming. For veterans of web programming, writing such code is as natural as breathing, but for beginners, they often have no clue about this issue. Therefore, I wrote this article to explain this issue in detail.
I. Paging principle:
The so-called paging display means that the result set in the database is manually divided into segments for display. Here two initial parameters are required:
How many records per page ($ PageSize )?
What is the current page ($ CurrentPageID )?
Now, you only need to give me another result set, so that I can display a specific result.
Other parameters, such as the previous page ($ PReviousPageID), next page ($ NextPageID), and total page ($ numPages), can all be obtained based on the front.
Take the MySQL database as an example. If you want to extract some content from the table, you can use the SQL statement: select * from table limit offset, rows. Take a look at the following SQL statements and try to find the rule rate.
First 10 records: select * from table limit 0, 10
11th to 20 records: select * from table limit 10, 10
21st to 30 records: select * from table limit 20, 10
......
This set of SQL statements is the SQL statement used to retrieve data of each page in the table when $ PageSize = 10. We can summarize this template:
Select * from table limit ($ CurrentPageID-1) * $ PageSize, $ PageSize
Use this template to compare the corresponding values with the preceding SQL statements to see if this is the case. After solving the most important problem of how to obtain data, the rest is simply passing parameters, constructing appropriate SQL statements, and then using php to obtain data from the database and display it.
Ii. Paging Code Description: Five Steps
The Code is fully explained and can be copied to your own notepad for direct use.
<Html>
Iii. Simple paging sharing
Now we will announce a simple classification production process. As long as you understand the principles and steps of this class, other complex classes will be able to bypass the class. The source code can be directly used in your project.
Database operation code: mysqli. func. php
<? Php // database connection constant define ('db _ host', 'localhost'); define ('db _ user', 'root'); define ('db _ pwd ', ''); define ('db _ name', 'Guest '); // connect to the database function conn () {$ conn = mysqli_connect (DB_HOST, DB_USER, DB_PWD, DB_NAME ); mysqli_query ($ conn, "set names utf8"); return $ conn;} // obtain the result set function doresult ($ SQL) {$ result = mysqli_query (conn (), $ SQL); return $ result;} // convert result set to function dolists ($ result) {return mys Qli_fetch_array ($ result, MYSQL_ASSOC);} function totalnums ($ SQL) {$ result = mysqli_query (conn (), $ SQL); return $ result-> num_rows ;} // disable the database function closedb () {if (! Mysqli_close () {exit ('close exception') ;}}?>
Paging implementation code:
<? Php include 'mysqli. func. php '; // total number of records $ SQL = "SELECT dg_id FROM tb_user"; $ totalnums = totalnums ($ SQL); // The number of records displayed on each page $ fnum = 8; // page flip $ pagenum = ceil ($ totalnums/$ fnum); // page number constant @ $ tmp = $ _ GET ['page']; // prevent malicious page flip if ($ tmp> $ pagenum) echo "<script> window. location. href = 'index. php' </script> "; // calculate the start value of the page if ($ tmp =" ") {$ num = 0 ;} else {$ num = ($ tmp-1) * $ fnum;} // query statement $ SQL = "SELECT dg_id, dg_user Name FROM tb_user order by dg_id desc limit ". $ num.", $ fnum "; $ result = doresult ($ SQL); // traverse the output while (! ! $ Rows = dolists ($ result) {echo $ rows ['dg _ id']. "". $ rows ['dg _ username']. "<br>" ;}// flip link for ($ I = 0; $ I <$ pagenum; $ I ++) {echo "<a href = index. php? Page = ". ($ I + 1)."> ". ($ I + 1)." </a> ";}?>
The above is all the content in this article. I hope it will be helpful for your learning and support for scripts.