This article mainly for everyone in detail introduced the PHP paging principle, PHP page code, PHP page-making tutorial, with a certain reference value, interested in small partners can refer to
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
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 constant define (' db_host ', ' localhost '); Define (' Db_user ', ' root '); Define (' db_pwd ', '); Define (' Db_name ', ' guest '); Connect database Function conn () { $conn = Mysqli_connect (Db_host, Db_user, Db_pwd, db_name); Mysqli_query ($conn, "Set names UTF8"); return $conn; } //Get 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 Database function Closedb () { if (! Mysqli_close ()) { exit (' close exception ');} } ? >
Pagination Implementation code:
<?php include ' mysqli.func.php '; Total number of records $sql = "Select dg_id from Tb_user"; $totalnums = Totalnums ($sql); Shows the number of bars per page $fnum = 8; Number of pages $pagenum = ceil ($totalnums/$fnum); Page constant @ $tmp = $_get[' page ']; Prevent malicious paging if ($tmp > $pagenum) echo "<script>window.location.href= ' index.php ' </script>"; Calculates the paging start value if ($tmp = = "") { $num = 0;} else { $num = ($tmp-1) * $FNUM; }//query statement $sql = "Select Dg_id,dg_username from Tb_user ORDER by dg_id DE SC 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>"; } ? >
Summary: The above is the entire content of this article, I hope to be able to help you learn.