首頁  >  文章  >  後端開發  >  PHP分頁類的定義與用法詳解

PHP分頁類的定義與用法詳解

墨辰丷
墨辰丷原創
2018-05-19 15:42:371611瀏覽

這篇文章主要介紹了PHP實現的分頁類定義與用法,結合具體實例形式分析了php封裝的分頁類常用操作及具體使用技巧,需要的朋友可以參考下

具體如下:

<?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;
   }
 }
?>

分頁樣式如下圖所示:

##使用說明:

<?php
  $_page = new Page($_total,$_pagesize);
  //其中 $_total 是数据集的总条数,$_pagesize 是每页显示的数量.
?>

相關推薦:

#實例詳解php分頁類別

TP5-分頁類別中的each函數 

#php基礎分頁類別程式碼詳解

#

以上是PHP分頁類的定義與用法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn