PHP碎碼——分頁類

來源:互聯網
上載者:User

標籤:php 分頁類



寫了兩個版本,還是後面好點,只計算關鍵資料,返回數組,html代碼例外拼接

<?php/** * 分頁類 * @author  timo * @version 2016-12-11 08:15 */class paginate{        //當前頁碼    private $page;    //記錄總條目    private $entry_total;    //每頁顯示多少個記錄    private $page_size;    //純數值li總數,即分頁欄的長度(不包括首尾上下頁)    private $page_li_count;    //總頁數    private $page_count;    private $result;    private $active;    //該url末尾必需帶有?或者&    private $url;        /**     *      * @param unknown $url  跳轉的連結地址     * @param unknown $entry_total  記錄的總條目     * @param number $page_size     每頁顯示多少個記錄     * @param number $page_li_count 分頁欄顯示多少個純數值的li (不包括首尾上下頁),即分頁欄的長度     */    public function __construct($entry_total,$page_size = 5,$page_li_count = 1){        //初始化成員變數,參數還需要仔細篩選        $this->entry_total = (int)$entry_total;        $this->page_size = (int)$page_size;        $this->page_count = ceil($entry_total/$page_size);        //分頁欄的長度取使用者提交的分頁欄長度與頁面總數中的較小值        $this->page_li_count = $page_li_count > $this->page_count ? $this->page_count : $page_li_count;            }        /**     * 設定當前頁及active狀態     * @param unknown $page 合法參數為:first end 1234     */    public function setPage($page){        if ($page == ‘end‘) {            $this->active = ‘end‘;            $this->page = $this->getPagecount();        }elseif ($page>0 && $page<=$this->page_count) {            $this->active = $page;            $this->page = $page;        }else{  //first或傳遞非法值的會被定到首頁去            $this->active = ‘first‘;            $this->page = 1;        }    }    /**     *      * @param unknown $url //該url末尾必需帶有?或者&     */    public function setURL($url){                $this->url = $url;    }    //當前頁碼    public function getPage(){        return $this->page;    }    //共計多少頁    public function getPagecount(){        return $this->page_count;    }    //共計多少條記錄    public function getEntrycount(){        return $this->entry_total;    }        /**     * 計算當前頁的分頁欄需要的資訊     * @return 返回數組,包含拼接分頁關鍵資訊     */    public function create(){        //首頁        $first_page= "first";        //計算上頁頁碼,最小為1        $pre_page = ($this->page-1)<=1 ? 1 : $this->page-1;        //計算下一頁頁碼,最大為page_count        $next_page = ($this->page+1)>=$this->page_count ? $this->page_count : $this->page+1;        //尾頁        $end_page= "end";        $nums_page = [];        //這個判斷與初始設定變數中的判斷重複了,但...安全點        if ($this->page_count >= $this->page_li_count) {            //中間li為第幾個,偶數取中偏前的一個            $mid_li = $this->page_li_count%2==0 ? $this->page_li_count/2 : (($this->page_li_count-1)/2)+1;            //理想情況下,$mid_li後面li應有的個數            $right_li_num = $this->page_li_count - $mid_li;            //理想情況下,$mid_li前面li應有的個數            $left_li_num = $mid_li-1;            //計算最右邊的頁碼            if ($this->page <= $left_li_num) {                //若當前頁左邊li個數不足以讓其置中,則最右端頁碼等於最右端li的編號                $right_li_page = $this->page_li_count;            }else {                $right_li_page = ($this->page_count-$this->page) >= $right_li_num ? $this->page+$right_li_num : $this->page_count;            }            for ($i = 0; $i < $this->page_li_count ; $i++) {                //看著可能有點繞,紙上算算就知道了,已知最右邊li的頁碼,li的總數,當前li的編號為$i+1,算當前li對應的頁碼                $n = $right_li_page-$this->page_li_count+$i+1;                $nums_page[] = $n;            }        }          //將這幾個拼接起來        $this->result[‘first‘] = $first_page;        $this->result[‘end‘] = $end_page;        $this->result[‘pre‘] = $pre_page;        $this->result[‘next‘] = $next_page;        $this->result[‘nums‘] = $nums_page;        $this->result[‘active‘] = $this->active;        return $this->result;    }            public function getHTML(){                //待完善........        $url = isset($this->url) ? $this->url : ‘?‘;                $first_class = $this->result[‘active‘] == ‘first‘ ? ‘active‘ : ‘‘;        $end_class = $this->result[‘active‘] == ‘end‘ ? ‘active‘ : ‘‘;        $pre_class = $this->result[‘active‘] == ‘pre‘ ? ‘active‘ : ‘‘;        $next_class = $this->result[‘active‘] == ‘next‘ ? ‘active‘ : ‘‘;          $first_page_html = <<<HTML<li class="$first_class"><a href="{$url}page={$this->result[‘first‘]}">首頁</a></li>HTML;        $pre_page_html = <<<HTML<li class="$pre_class"><a href="{$url}page={$this->result[‘pre‘]}">上一頁</a></li>HTML;        $next_page_html = <<<HTML<li class="$next_class"><a href="{$url}page={$this->result[‘next‘]}">下一頁</a></li>HTML;        $end_page_html = <<<HTML<li class="$end_class"><a href="{$url}page={$this->result[‘end‘]}">尾頁</a></li>HTML;        $lis_page_html = ‘‘;        foreach ($this->result[‘nums‘] as $v){            $li_class =  $this->result[‘active‘] == $v ? ‘active‘ : ‘‘;            $lis_page_html .= <<<HTML<li class="$li_class"><a href="{$url}page=$v">$v</a></li>HTML;        }                $htmlcode =  $first_page_html.$pre_page_html.$lis_page_html.$next_page_html.$end_page_html;                return $htmlcode;            }    }/*********** * <?phprequire ‘paginate02.class.php‘;$a = new paginate(10,5,4);$a-> setPage( isset($_GET[‘page‘])?$_GET[‘page‘]:1);$a->create();?><!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>fenye</title>    <link rel="stylesheet" href="css/bootstrap.css"></head><body>    <ul class="pagination">         <?php echo $a->getHTML()?>    </ul></body></html> *  */


本文出自 “樵夫” 部落格,請務必保留此出處http://woodcutter.blog.51cto.com/9079117/1881779

PHP碎碼——分頁類

聯繫我們

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