select * from table limit 0,10 // The first 10 records
select * from table limit 10,10 // The 11th to 20th records
select * from table limit 20,10 // The 21st to 20th records 30 records
...
Copy code
This set of sql statements is actually the sql statement for fetching each page of data in 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 and compare it with 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.
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 ){ //The remainder of dividing the total data amount by the number of pages
$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, rounded up and plus one
}else{
$page_count = $amount / $page_size; //If there is no remainder, then The number of pages is equal to the total amount of data divided by the number of pages per 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. You can simply use the obtained two-dimensional array to display the results using foreach
?>
Copy the code
oo style code, the database connection in the following code is processed using the pear db class
// filename: pager.class.php
// Paging class, this class is only used to process data structures and is not responsible for processing display work
class pager
{
var $pagesize ; //Number of each page
var $currentpageid; //Current page number
var $nextpageid; //Next page
var $previouspageid; //Previous page
var $numpages; //Total number of pages
var $numitems; //Total number of records
var $isfirstpage; //Whether it is the first page
var $islastpage; //Whether it is the last page
var $sql; //SQL query statement
function pager($option)
{
global $db;
$this->_setoptions($option);
// Total number of items
if ( !isset($this->numitems) )
{
$res = $db->query( $this->sql);
$this->numitems = $res->numrows();
}
// Total number of pages
if ( $this->numitems > 0 )
{
if ( $this->numitems < $this->pagesize ){ $this->numpages = 1; }
* Returns the database connection of the result set
* When the result set is relatively large, you can directly use this method to obtain the database connection, and then traverse outside the class, which has less overhead
* If the result set is not very large, you can Directly use getpagedata to obtain the result in two-dimensional array format
* The getpagedata method also calls this method to obtain the result
Instructions:
This class only processes data and is not responsible for display, because it is a bit reluctant to put both data processing and result display into one class.
The situation and requirements during display are changeable. It is better to process it according to the results given by the class. A better way is to inherit a subclass of your own based on the pager class to display different paginations. For example, to display the user pagination list:
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