Home >Backend Development >PHP Tutorial >That's all the so-called PHP paging_PHP Tutorial

That's all the so-called PHP paging_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:37:431600browse

The so-called paging display means that the result set in the database is artificially divided into segments for display. Two initial parameters are required here:
How many records per page ($PageSize)?
What page is the current page ($CurrentPageID)?
Now just give me another result set and I can display a specific result.
As for other parameters, such as: previous page ($PReviousPageID), next page ($NextPageID), total number of pages ($numPages), etc., they can all be obtained based on the previous things.
Taking the MySQL database as an example, if you want to intercept a certain piece of content from the 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 the rules.
The first 10 records: select * from table limit 0,10
Records 11 to 20: select * from table limit 10,10
Records 21 to 30: select * from table limit 20,10

This set of sql statements is actually the sql statement for fetching data from each page of the table when $PageSize=10. We can summarize such a template:
select * from table limit ($CurrentPageID - 1) * $PageSize, $PageSize
Take this template and substitute the corresponding values ​​​​into the above set of SQL statements to see if that is the case. After solving the most important problem of how to obtain the data, all that is left is to pass the parameters, construct the appropriate SQL statement and then use PHP to obtain the data from the database and display it. Below I will explain it with specific code.
3. Simple code
Please read the following code in detail, debug and run it yourself, it is best to modify it and add your own functions, such as search, etc.
[php]
// Establish database connection
$link = mysql_connect("localhost", "mysql_user", "mysql_passWord")
or die("Could not connect: " . mysql_error());
// Get the current page number
if( isset($_GET['page']) ){
$page = intval( $_GET['page'] );
}
else{
$page = 1;
}
//Number per page
$PageSize = 10;
// Get the total data volume
$sql = "select count(*) as amount from table";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$amount = $row['amount'];
// Calculate how many pages there are in total
if( $amount ){
if( $amount < $page_size ){ $page_count = 1; } //If the total data amount is less than $PageSize, then there is only one page
If ($ amount % $ page_size) {// Take the total data volume and divide the number per page
         $page_count = (int)($amount / $page_size) + 1;                                                                                                                                                                                 // If there is a remainder, the number of pages is equal to the total data amount divided by the number of pages per page, rounded up and plus one
}else{
$page_count = $amount / $page_size; //If there is no remainder, the number of pages is equal to the total data amount divided by the number of pages
}  
}
else{
$page_count = 0;
}
// Page turning link
$page_string = '';
if( $page == 1 ){
$page_string .= 'First page|Previous page|';
}
else{
$page_string .= 'First page|Previous page|';
}
if( ($page == $page_count) || ($page_count == 0) ){
$page_string .= 'Next page|Last page';
}
else{
$page_string .= '
Next page| Last page';
}
// Get data and return the result 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, so that is beyond the scope of the discussion. As long as you use foreach, you can easily use the obtained two-dimensional array to display the results
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735160.htmlTechArticleThe so-called paging display means that the result set in the database is artificially divided into sections for display. Here we need two Initial parameters: How many records per page ($PageSize)? Currently...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn