Paging display is a very common method of browsing and displaying large amounts of data, and is one of the most commonly processed events in web programming. For veterans of web programming, writing this kind of code is as natural as breathing, but for beginners, they are often confused about this issue, so I specially wrote this article to explain this issue in detail.
1. Paging principle:
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:
Each How many records are in the page ($PageSize)?
What page is the current page ($CurrentPageID)?
Now as long as you give me another result set, 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 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
The 11th to 20th records: select * from table limit 10,10
The 21st to 30th records: select * from table limit 20,10
......
This set of sql statements is actually the sql statement to fetch 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 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.
This article mainly introduces the paging class implemented by PHP. It analyzes the common operations and specific usage techniques of the paging class encapsulated in PHP based on specific examples. Friends in need can refer to the following, the details are as follows:
<?php class Page { private $total; //总记录 private $pagesize; //每页显示多少条 private $limit; //limit private $page; //当前页码 private $pagenum; //总页码 private $url; //地址 private $bothnum; //两边保持数字分页的量 //构造方法初始化 public function construct($_total, $_pagesize) { $this->total = $_total ? $_total : 1; $this->pagesize = $_pagesize; $this->pagenum = ceil($this->total / $this->pagesize); $this->page = $this->setPage(); $this->limit = "LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize"; $this->url = $this->setUrl(); $this->bothnum = 2; } //拦截器 private function get($_key) { return $this->$_key; } //获取当前页码 private function setPage() { if (!empty($_GET['page'])) { if ($_GET['page'] > 0) { if ($_GET['page'] > $this->pagenum) { return $this->pagenum; } else { return $_GET['page']; } } else { return 1; } } else { return 1; } } //获取地址 private function setUrl() { $_url = $_SERVER["REQUEST_URI"]; $_par = parse_url($_url); if (isset($_par['query'])) { parse_str($_par['query'],$_query); unset($_query['page']); $_url = $_par['path'].'?'.http_build_query($_query); } return $_url; } //数字目录 private function pageList() { for ($i=$this->bothnum;$i>=1;$i--) { $_page = $this->page-$i; if ($_page < 1) continue; $_pagelist .= ' <a href="'.$this->url.'&page='.$_page.'" rel="external nofollow" rel="external nofollow" >'.$_page.'</a> '; } $_pagelist .= ' <span class="me">'.$this->page.'</span> '; for ($i=1;$i<=$this->bothnum;$i++) { $_page = $this->page+$i; if ($_page > $this->pagenum) break; $_pagelist .= ' <a href="'.$this->url.'&page='.$_page.'" rel="external nofollow" rel="external nofollow" >'.$_page.'</a> '; } return $_pagelist; } //首页 private function first() { if ($this->page > $this->bothnum+1) { return ' <a href="'.$this->url.'" rel="external nofollow" >1</a> ...'; } } //上一页 private function prev() { if ($this->page == 1) { return '<span class="disabled">上一页</span>'; } return ' <a href="'.$this->url.'&page='.($this->page-1).'" rel="external nofollow" >上一页</a> '; } //下一页 private function next() { if ($this->page == $this->pagenum) { return '<span class="disabled">下一页</span>'; } return ' <a href="'.$this->url.'&page='.($this->page+1).'" rel="external nofollow" >下一页</a> '; } //尾页 private function last() { if ($this->pagenum - $this->page > $this->bothnum) { return ' ...<a href="'.$this->url.'&page='.$this->pagenum.'" rel="external nofollow" >'.$this->pagenum.'</a> '; } } //分页信息 public function showpage() { $_page .= $this->first(); $_page .= $this->pageList(); $_page .= $this->last(); $_page .= $this->prev(); $_page .= $this->next(); return $_page; } } ?>
The pagination style is as shown in the figure below:
Instructions for use:
<?php $_page = new Page($_total,$_pagesize); //其中 $_total 是数据集的总条数,$_pagesize 是每页显示的数量. ?>
The above is the detailed content of Sample code of paging class implemented in php. For more information, please follow other related articles on the PHP Chinese website!