Home  >  Article  >  Backend Development  >  Sample code of paging class implemented in php

Sample code of paging class implemented in php

怪我咯
怪我咯Original
2017-07-14 09:22:121212browse

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[&#39;page&#39;])) {
        if ($_GET[&#39;page&#39;] > 0) {
          if ($_GET[&#39;page&#39;] > $this->pagenum) {
             return $this->pagenum;
          } else {
             return $_GET[&#39;page&#39;];
          }
        } else {
          return 1;
        }
     } else {
        return 1;
     }
   }
   //获取地址
   private function setUrl() {
     $_url = $_SERVER["REQUEST_URI"];
     $_par = parse_url($_url);
     if (isset($_par[&#39;query&#39;])) {
        parse_str($_par[&#39;query&#39;],$_query);
        unset($_query[&#39;page&#39;]);
        $_url = $_par[&#39;path&#39;].&#39;?&#39;.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 .= &#39; <a href="&#39;.$this->url.&#39;&page=&#39;.$_page.&#39;" rel="external nofollow" rel="external nofollow" >&#39;.$_page.&#39;</a> &#39;;
     }
     $_pagelist .= &#39; <span class="me">&#39;.$this->page.&#39;</span> &#39;;
     for ($i=1;$i<=$this->bothnum;$i++) {
      $_page = $this->page+$i;
        if ($_page > $this->pagenum) break;
        $_pagelist .= &#39; <a href="&#39;.$this->url.&#39;&page=&#39;.$_page.&#39;" rel="external nofollow" rel="external nofollow" >&#39;.$_page.&#39;</a> &#39;;
     }
     return $_pagelist;
   }
   //首页
   private function first() {
     if ($this->page > $this->bothnum+1) {
        return &#39; <a href="&#39;.$this->url.&#39;" rel="external nofollow" >1</a> ...&#39;;
     }
   }
   //上一页
   private function prev() {
     if ($this->page == 1) {
        return &#39;<span class="disabled">上一页</span>&#39;;
     }
     return &#39; <a href="&#39;.$this->url.&#39;&page=&#39;.($this->page-1).&#39;" rel="external nofollow" >上一页</a> &#39;;
   }
   //下一页
   private function next() {
     if ($this->page == $this->pagenum) {
        return &#39;<span class="disabled">下一页</span>&#39;;
     }
     return &#39; <a href="&#39;.$this->url.&#39;&page=&#39;.($this->page+1).&#39;" rel="external nofollow" >下一页</a> &#39;;
   }
   //尾页
   private function last() {
     if ($this->pagenum - $this->page > $this->bothnum) {
        return &#39; ...<a href="&#39;.$this->url.&#39;&page=&#39;.$this->pagenum.&#39;" rel="external nofollow" >&#39;.$this->pagenum.&#39;</a> &#39;;
     }
   }
   //分页信息
   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!

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