php實現的分頁工具類

來源:互聯網
上載者:User

 本文分享一個php實現的分頁工具類,代碼內有注釋,大家參考使用吧

代碼:  代碼如下:    /**    * 把中間的看成一個可以滑動的固定長度的尺子    *    * 把$this->_totalShowPages 當成一個可以滑動的固定長度尺子,    * 然後$this->_totalPages就是一個給定長度的木塊,尺子在這個    * 木塊上滑動。情況兩種:    * 1. 尺子長度大於木塊長度,那麼就直接輸出所有的頁碼;    * 2. 尺子長度小於木塊長度,那麼就只用找到輸出這個尺子長度頁    * 數的起始點——$start, $end;    * @Access protected    * @Return void    * @Exception none    */    protected function _getShowPageNumber()    {    $pageHtml = '';    //找到$start點    if($this->_curPage - 2 > 1) {    $start = $this->_curPage - 2;    } else {    $start = 1;    }    //找到$end 點    $end = $start + $this->_totalShowPages;    if($end >= $this->_totalPages) {    $end = $this->_totalPages;    $start = $end - $this->_totalShowPages; //保證頁面顯示的長度為$this->_totalShowPages    }    if($start != 1) {    $pageHtml .= $this->_getPageHtml(1);    $preMore = $this->_curPage - $this->_totalShowPages;    if($preMore < 1) {    $preMore = 1;    }    $pageHtml .= $this->_getMorePageHtml($preMore);    }    for($page = $start; $page < $end; $page ++) {    $pageHtml .= $this->_getPageHtml($page);    }    if($end != $this->_totalPages) {    $pageHtml .= $this->_getMorePageHtml($end);    }    $pageHtml .= $this->_getNormalPageHtml($this->_totalPages);     return $pageHtml;    }  第一種老想法的代碼實現: 代碼如下:    /**    * 一步步的來    *    * @desc    *    * @Access protected    * @Return void    * @Exception none    */    protected function _getShowPageNumberTwo()    {    if($this->_curPage < $this->_totalShowPages) {    for($page = 1; $page < $this->_totalShowPages; $page ++) {    $pageHtml .= $this->_getPageHtml($page);    }    $pageHtml .= $this->_getMorePageHtml($this->_totalShowPages);    $pageHtml .= $this->_getNormalPageHtml($this->_totalPages);    } else {    $pageHtml .= $this->_getNormalPageHtml(1);    if($this->_curPage == $this->_totalShowPages) {    $pageHtml .= $this->_getMorePageHtml(1);    } else {    $pageHtml .= $this->_getMorePageHtml($this->_curPage - $this->_totalShowPages);    }    if($this->_curPage + $this->_totalShowPages >= $this->_totalPages) {    for($page = $this->_totalPages - $this->_totalShowPages; $page < = $this->_totalPages; $page ++) {    $pageHtml .= $this->_getPageHtml($page);    }    } else {    $start = $this->_curPage - 2;    $end = $this->_curPage + $this->_totalShowPages - 2;    for($page = $start; $page < $end; $page ++) {    $pageHtml .= $this->_getPageHtml($page);    }    $pageHtml .= $this->_getMorePageHtml($this->_curPage + $this->_totalShowPages - 2);    $pageHtml .= $this->_getNormalPageHtml($this->_totalPages);    }    }     return $pageHtml;    } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.