1. Preface
For the veteran of Web programming, writing this code is just as natural as breathing, but for beginners, often do not have a clue to this problem, so specially written this article on this issue to explain in detail, and strive to let the friends after reading this post after reading for the page display of the principle and implementation of the method have some understanding. This article is for beginners to read, and all sample code is written using PHP.
2, PHP article paging principle
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. I'll use the specific code to illustrate the following.
3, PHP article paging simple code
Please read the following code in detail, run your own debugging once, it is best to modify it once, plus your own features, such as search and so on.
- < ? PHP
- Establishing a database connection
- $ Link = mysql_connect ("localhost",
"Mysql_user", "Mysql_password")
- Or Die ("Could Not connect:". Mysql_error ());
- Get the current number of pages
- if (Isset ($_get[' page ')) {
- $ page = intval ($_get[' page ');
- }
- else{
- $ page = 1 ;
- }
- Quantity per page
- $ PageSize = Ten ;
- Get Total data volume
- $ SQL = "SELECT COUNT (*) as amount from table" ;
- $ result = mysql_query ($sql);
- $ Row = Mysql_fetch_row ($result);
- $ Amount = $row [' Amount '];
- Count how many pages there are in total
- if ($amount) {
- if ($amount < $page _size) {$page_count = 1 ; }
- If the total data volume is less than $pagesize, then there is only one page
- if ($amount% $page _size) {
- The total amount of data divided by the remainder of each page
- $ Page_count = (int) ($amount/$page _size) + 1;
- If there is an excess, the number of pages equals the total amount of data divided by each page
Take the whole fruit and add one more
- }else{
- $ Page_count = $amount/$page _size;
- If there is no remainder, the number of pages equals the total data amount divided by the result of each page
- }
- }
- else{
- $ Page_count = 0 ;
- }
- Page link
- $ page_string = '' ;
- if ($page = = 1) {
- $page _string . = ' first page | previous page | ' ;
- }
- else{
- $page _string . = ' first page
| . ($page-1). ' > Previous Page a>| ';
- }
- if (($page = = $page _count) | | ($ Page_count = = 0)) {
- $page _string . = ' next page | last ' ;
- }
- else{
- $page _string . = '.( $page + 1). ' >
Next Page a>| < a href=? page = '. $page _count. ' > last page a>';
- }
- Get data to return results in two-dimensional array format
- if ($amount) {
- $ SQL = "SELECT * FROM table ORDER BY id DESC
Limit ". ($page-1) * $page _size. ", $page _size";
- $ result = mysql_query ($sql);
- While ($row = mysql_fetch_row($result)) {
- $rowset [] = $row;
- }
- }else{
- $ Rowset = Array ();
- }
- There is no code to display the results, that is not the scope of the discussion,
As long as you use foreach, you can simply use the resulting two-dimensional array to display the results
- ?>
The above is the PHP article paging related implementation methods.
http://www.bkjia.com/PHPjc/446034.html www.bkjia.com true http://www.bkjia.com/PHPjc/446034.html techarticle 1, preface for the veteran of Web programming, writing this code is just as natural as breathing, but for beginners, often the problem is not a clue, so specially written ...