The article is focused on the principle of paging in PHP, at the same time we are using an implementation to explain how to implement paging in PHP, this is based on their own written to leave the square board program, a need for friends can refer to, very suitable for beginners oh.
The key issue of paging is actually a key word for MySQL limite the two numbers followed by this keyword, the first one is the number of start lines that need to be recorded, and the second number is a few lines from the beginning of the line-this is not to be mistaken for a distance from the beginning of the first line to the end of the first line.
Look at the 19th line of our index.php:
| The code is as follows |
Copy Code |
$RECORDSQL = $sql. "LIMIT". $page * $pagesize. ",". $pagesize; |
The judgment of the variable $page is obtained in line 8th and 9th of the program:
| The code is as follows |
Copy Code |
if (Isset ($_get[' page ') &&$_get[' page ']!= ") $page =$_get[' page ']; else $page = 0; |
Of course, you see the $_get[' page ' variable.
The variable $pagesize we define in line 7th, which is how many records are displayed per page, we define 10. When the variable $page=0, we will end up with this:
| The code is as follows |
Copy Code |
$RECORDSQL = "Select a. *, B.name, B.email, B.qq, C.revert_time, C.revert From Post a Left JOIN revert c on (a.id = c.post_id), Guest B WHERE a.guest_id = b.id ORDER by a.ID DESC "LIMIT 0,10";
|
is to start with 0 records to obtain 10 records of the results-this is included in the No. 0 record;
What about when the $page=1? It is conceivable-this time $page* $pagesize = 10, of course, from ... Don't say it, say it again.
There is also need to say, why we have to add this limit after the total number of rows, it is very simple, if we get the result record number of this limit, we will never be greater than 10, because the number of records he returned after the most is 10 ah brother. This is obviously not in line with the actual situation. So we were doing the following:
| The code is as follows |
Copy Code |
Mysql_num_rows (mysql_query ($sql));
|
The total number of records is obtained before the Limit keyword is added.
Okay, we've got the total number of records, set the number of records displayed per page 10, now we need only the total page, this elementary school question I will not say? Of course, the total number of records divided by the number of pages displayed is the total page--of course there are countless times, such as the total number of records 11 per page shows 10, we need to divide several pages? Two pages, of course, how can we make 11/10=2? It's obviously wrong, but real life is not perfect math. Use the Ceil function Ah, this previous chapter also said, did not say.
Let's see what we have now:
| The code is as follows |
Copy Code |
Total Record count, $numRecord = Mysql_num_rows (mysql_query ($sql)); Total pages, $totalpage = Ceil ($numRecord/$pagesize); |
We don't need anything. How to display "previous page next page", very simple, two judgment is done, look at the index.php 159-163 lines:
Line 160th:
| The code is as follows |
Copy Code |
if ($page >0) echo "prev |";
|
If the variable $page>0, that is, the current page number is greater than 0 indicates that there is a previous page, then the "previous page" of the link, the link inside the $page need to reduce one, for example, the current page is 2, then the previous page of course 1 is not, when we click on the previous page, As this link will pass through the page=1, this way of relying on links to pass variables is the Get method. This goes back to the program 8 lines 9 to be processed.
In line 161, we make the judgment that if the current page is less than the total number of records -1--because our page numbers are starting from 0, the total number of records must be reduced by one to be judged to match the actual. If this judgment is set, there is the next page, the next page $page of course to add one.
All right, just remember, how to get the total number of records, how to add the Limit keyword to the SQL statement, how to tell the page up and down, everything OK
The complete code
| The code is as follows |
Copy Code |
$pagesize = 10;//How many message records are displayed on each page if (Isset ($_get[' page ')) &&$_get[' page ']!= ') $page =$_get[' page ']; else $page = 0; $sql = "Select a. *, B.name, B.email, B.qq, C.revert_time, C.revert From Post a Left JOIN revert c on (a.id = c.post_id), Guest B WHERE a.guest_id = b.id ORDER by a.ID DESC "; $numRecord = Mysql_num_rows (mysql_query ($sql)); $totalpage = Ceil ($numRecord/$pagesize); $RECORDSQL = $sql. "LIMIT". $page * $pagesize. ",". $pagesize; $result = mysql_query ($RECORDSQL);
while ($rs =mysql_fetch_object ($result)) {?>
| Message person: name?> | Email: email?>| QQ: qq?>| message time: post_time+8*3600)?> |
if (isset ($_session[' login ') &&$_session[' login ') {?>
| Id?> "> Reply | id?> "> Delete |
}?>
Message content: post))?>
Reply content: revert) >[ revert_time!= "") Echo Date ("Y-m-d h:i:s", $rs->revert_time+8*3600)?>]
|
|
}?>
if ($page >0) echo "prev |"; if ($page < $totalpage-1) echo "next page"; ?> |
|
http://www.bkjia.com/PHPjc/631302.html www.bkjia.com true http://www.bkjia.com/PHPjc/631302.html techarticle The article is focused on the principle of paging in PHP, at the same time we are using an implementation to explain how to implement paging in PHP, this is based on their own writing to save the square program, the need for friends ...