Pagination display is a very common way to browse and display large amounts of data, which is one of the most frequently handled events in Web programming. For the veteran of Web programming, writing this code is just as natural as breathing, but for beginners, there is often no clue to the problem, so write this article specifically to explain the problem in detail.
first, the principle of paging:
The so-called paging display, that is, the result set in the database is artificially divided into a section of the display, here requires two initial parameters:
How many records per page ($PageSize)?
What is the current page ($CurrentPageID)?
Now just give me a result set and I can show a specific result.
For other parameters, such as the previous page ($PReviousPageID), the next page ($NextPageID), the total number of pages ($numPages), and so on, can be based on the front of these things.
For example, in MySQL database, if you want to intercept a piece of content from within a 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 out the rate of compliance.
Top 10 Records: SELECT * FROM table limit 0,10
11th to 20th record: SELECT * FROM table limit 10,10
21st to 30th Record: SELECT * FROM table limit 20,10
......
This set of SQL statements is actually a SQL statement that takes every page of data in the table when $pagesize=10, and we can summarize a template like this:
SELECT * FROM table limit ($CurrentPageID-1) * $PageSize, $PageSize
Take this template and replace the corresponding value with the set of SQL statements above to see if that's the case. Take care of the most important question of how to get the data, all that is left is to pass the parameters, construct the appropriate SQL statement, and then use PHP to retrieve the data from the database and display it.
Two, paging code Description: Five Steps
The code is fully explained and can be copied to your own Notepad directly using
- <meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
- <title> Employee Information List </title>
- <?php
- Display information for all EMP tables
- 1. Connect to the database
- $conn =mysql_connect (' localhost ', ' root ', ' 1234abcd ') or Die (' Connection Database error '. mysql_error ());
- 2. Select a database
- mysql_select_db (' empmanage ');
- 3. Select a character Set
- mysql_query (' Set names UTF8 ');
- 4. Send SQL statements and get results for processing
- 4.1 Paging [paging to emit two SQL statements, one to get $rowcount, and one to get paged results through the limit of SQL. So we're going to get two result sets and remember to distinguish them when naming them.
- Paging (four values of two SQL statements). ]
- $pageSize =3;//How many records are displayed per page
- How many records are there in $rowCount =0;//?
- $pageNow =1;//want to display the first few pages
- $pageCount =0;//altogether have how many pages [pagination altogether this four indicators, is indispensable. Since $rowcount can be obtained from the server, it can be given an initial value of 0;
- $pageNow want to display the first few pages, it is best to set to 0, $pageSize is the number of records per page, which is based on the requirements of the site in advance.
- $pageCount =ceil ($rowCount/$pageSize), since $rowcount can have an initial value of 0, the $pagecount can of course be set to 0. Four indicators, two 0, one 1, and the other for website requirements. ]
- 4.15 Modify the value of the $pagenow according to the paging link
- if (!empty ($_get[' Pagenow ')) {
- $pageNow =$_get[' Pagenow '];
- }[modifies the value of $pagenow according to the paging link. ]
- $sql = ' SELECT count (ID) from EMP ';
- $res 1=mysql_query ($sql);
- 4.11 Number of rows removed
- if ($row =mysql_fetch_row ($res 1)) {
- $rowCount = $row [0];
- }//[get $rowcount,, into the We will know $pagecount these two indicators. ]
- 4.12 Calculate the total number of pages
- $pageCount =ceil ($rowCount/$pageSize);
- $pageStart = ($pageNow-1) * $pageSize;
- 4.13 Sending SQL results with paging
- $sql = "SELECT * from emp limit $pageStart, $pageSize";//[paging is implemented based on the two values (starting value, per page number) following the limit of the $sql statement. And the two values are evaluated. ]
- $res 2=mysql_query ($sql, $conn) or die (' cannot get result set '. Mysql_error ());
- Echo ' <table border=1> '; [echo "<table border= ' 1px ' cellspacing= ' 0px ' bordercolor= ' red ' width= ' 600px ' >";]
- "<tr><th>id</th><th>name</th><th>grade</th><th>email</th ><th>salary</th><th><a href= ' # ' > Delete user </a></th><th><a href= ' # ' > Modify User </a></th></tr> "; while ($row =mysql_fetch_assoc ($res 2)) {
- echo "<tr><td>{$row [' id ']}</td><td>{$row [' name ']}</td><td>{$row [' Grade ']} </td><td>{$row [' email ']}</td><td>{$row [' Salary ']}</td><td><a href= ' # ' > Delete User </a></td><td><a href= ' # ' > Modify User </a></td></tr> "; }
- Echo ' </table> ';
- 4.14 Print out a hyperlink to a page number
- for ($i =1; $i <= $pageCount; $i + +) {
- echo "<a href= '? pagenow= $i ' > $i </a>"//[print out the page number of hyperlinks]
- }
- 5. Release the resource, close the connection
- Mysql_free_result ($res 2);
- Mysql_close ($conn);
- ?>
Copy Code
Third, simple paging class sharing
Now publish a simple sorting production. As long as you understand the principles and procedures of this class, other complex classes can be comprehend by analogy. No nonsense, directly on the source, you can directly use in your project.
Database Operation Class Code: mysqli.func.php
- <?php
- Database Connection Constants
- Define (' db_host ', ' localhost ');
- Define (' Db_user ', ' root ');
- Define (' db_pwd ', ');
- Define (' Db_name ', ' guest ');
- Connecting to a database
- Function Conn ()
- {
- $conn = Mysqli_connect (Db_host, Db_user, Db_pwd, db_name);
- Mysqli_query ($conn, "Set names UTF8");
- return $conn;
- }
- Get the result set
- function Doresult ($sql) {
- $result =mysqli_query (Conn (), $sql);
- return $result;
- }
- Result set to object collection
- function Dolists ($result) {
- Return Mysqli_fetch_array ($result, MYSQL_ASSOC);
- }
- function Totalnums ($sql) {
- $result =mysqli_query (Conn (), $sql);
- return $result->num_rows;
- }
- Close the database
- function Closedb ()
- {
- if (! Mysqli_close ()) {
- Exit (' close exception ');
- }
- }
- ?>
Copy Code
Pagination Implementation code:
What's hidden in this post
- <?php
- Include ' mysqli.func.php ';
- Total Record Count
- $sql = "Select dg_id from Tb_user";
- $totalnums = Totalnums ($sql);
- Show number of bars per page
- $fnum = 8;
- Number of pages
- $pagenum = Ceil ($totalnums/$fnum);
- Page Constants
- @ $tmp = $_get[' page '];
- Prevent malicious paging
- if ($tmp > $pagenum)
- echo "<script>window.location.href= ' index.php ' </script>";
- Calculate Paging Start value
- if ($tmp = = "") {
- $num = 0;
- } else {
- $num = ($tmp-1) * $FNUM;
- }
- Query statements
- $sql = "Select Dg_id,dg_username from Tb_user ORDER by dg_id DESC LIMIT". $num. ", $fnum";
- $result = Doresult ($sql);
- Traverse output
- while (!! $rows = dolists ($result)) {
- echo $rows [' dg_id ']. " " . $rows [' Dg_username ']. "<br>";
- }
- Page link
- for ($i = 0; $i < $pagenum; $i + +) {
- echo "<a href=index.php?page=". ($i + 1). ">". ($i + 1). "</a>";
- }
- ?>
Copy Code
PHP Paging principle + page code + pagination class production