Home >Backend Development >PHP Tutorial >Paging source and paging class in php_PHP tutorial
The so-called paging display means that the result set in the database is artificially divided into sections 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]
The code is as follows | Copy code | ||||
|
PHP paginator production, automatically generates faceted page numbers, JS calls functions
The code is as follows | Copy code |
class PageView{ /**Is there a previous page**/ public $pageNoList = array(); public $jsFunction ='jsFunction'; $this->totalNum = $count;//Total number of records //Calculate offset $this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true; $this->pageData = $pageData; } }else if($this->pageNo > $this->pageCount - 4){ array_push($pageList,-1); array_push($pageList,$this->pageNo -2); array_push($pageList,-1); } /*** $pageString =" ";
if(!empty($pageList)){ if($this->hasNextPage){ } return $pageString; } } ?> |
PHP调用
代码如下
|
复制代码 | ||||
if(empty($pageNo)){