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. 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 = 10;
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) {//Take 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 the result of each page plus one
}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 | prev | ';
}
else{
$page _string. = ' first page | prev | ';
}
if ($page = = $page _count) | | ($page _count = = 0)) {
$page _string. = ' next page | last ';
}
else{
$page _string. = ' next page | last ';
}
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 a foreach can be used to easily use the resulting two-dimensional array to display the results
?>
http://www.bkjia.com/PHPjc/735160.html www.bkjia.com true http://www.bkjia.com/PHPjc/735160.html techarticle The so-called paging display, that is, the result set in the database is artificially divided into a section of the display, here need two initial parameters: how many records per page ($PageSize)? The current is ...