PHP實現分頁類的定義與用法

來源:互聯網
上載者:User
本文主要和大家介紹了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['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;   } }?>

分頁樣式如所示:

使用說明:


<?php  $_page = new Page($_total,$_pagesize);  //其中 $_total 是資料集的總條數,$_pagesize 是每頁顯示的數量.?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.