php分頁類代碼和使用


在這要說的一點就是如果你做的分頁是有搜索條件的,那么就可以用下面的代碼,然后調用之后在new Page()里面把連接加進去就可以了。例如:

$href="http://www.***.cn/***/***/***?city=$city&educ=$educ&exp=$exp&money=$money&page=";

$n_page=new Page(總頁數,顯示頁數,當前頁碼,每頁顯示條數,$href);

就可以了。

如果做的分頁沒有用到搜索條件的話那么就將代碼中的$_GET['page']換成http_build_query($_GET)即可。

http_build_query — 生成 URL-encode 之后的請求字符串

 

 

下面的是分頁類

<?php
/**
 * 分頁類
 *
 * 調用方式:
 * $p=new Page(總頁數,顯示頁數,當前頁碼,每頁顯示條數,[鏈接]);
 * print_r($p->getPages()); //生成一個頁碼數組(鍵為頁碼,值為鏈接)
 * echo $p->showPages(1);    //生成一個頁碼樣式(可添加自定義樣式)
 *
 * @author: 草根級
 * @Last Modified time: 2017-01-09 13:31
 */

/*
思路:
給我一個 總頁數,需要顯示的頁數,當前頁,每頁顯示的條數,連接
寫一個方法 生成一個一維數組,鍵為頁碼 值為連接
寫一個方法 返回一個生成好樣式的頁碼(並且可以根據自己需要添加樣式)
默認樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]
*/
class Page{
    protected $count;       //總條數
    protected $showPages;   //需要顯示的頁數
    protected $countPages;  //總頁數
    protected $currPage;    //當前頁
    protected $subPages;    //每頁顯示條數
    protected $href;        //連接
    protected $page_arr=array();    //保存生成的頁碼 鍵頁碼 值為連接

    /**
     * __construct  構造函數(獲取分頁所需參數)
     * @param int $count     總條數
     * @param int $showPages 顯示頁數
     * @param int $currPage  當前頁數
     * @param int $subPages  每頁顯示數量
     * @param string $href   連接(不設置則獲取當前URL)
     */
    public function __construct($count,$showPages,$currPage,$subPages,$href=''){
        $this->count=$count;
        $this->showPages=$showPages;
        $this->currPage=$currPage;
        $this->subPages=$subPages;

        //如果鏈接沒有設置則獲取當前連接

            $this->href=$href;
        $this->construct_Pages();
    }

    /**
     * getPages 返回頁碼數組
     * @return array 一維數組 鍵為頁碼 值為鏈接
     */
    public function getPages(){
        return $this->page_arr;
    }

    /**
     * showPages 返回生成好的頁碼
     * @param  int $style 樣式
     * @return string     生成好的頁碼
     */
    public function showPages($style=1){
        $func='pageStyle'.$style;
        return $this->$func();
    }

    /**
     * pageStyle1 分頁樣式(可參照這個添加自定義樣式 例如pageStyle2())
     * 樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]
     * @return string
     */
    protected function pageStyle1(){
        /* 構造普通模式的分頁
        共4523條記錄,每頁顯示10條,當前第1/453頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]
        */
        $pageStr='共'.$this->count.'條記錄,每頁顯示'.$this->subPages.'條';
        $pageStr.='當前第'.$this->currPage.'/'.$this->countPages.'頁 ';

        $_GET['page'] = 1;
        $pageStr.='<span>[<a href="'.$this->href.''.$_GET['page'].'">首頁</a>] </span>';
        //如果當前頁不是第一頁就顯示上頁
        if($this->currPage>1){
            $_GET['page'] = $this->currPage-1;
            $pageStr.='<span>[<a href="'.$this->href.''.$_GET['page'].'">上頁</a>] </span>';
        }

        foreach ($this->page_arr as $k => $v) {
            $_GET['page'] = $k;
            $pageStr.='<span>[<a href="'.$v.'">'.$k.'</a>] </span>';
        }

        //如果當前頁小於總頁數就顯示下一頁
        if($this->currPage<$this->countPages){
            $_GET['page'] = $this->currPage+1;
            $pageStr.='<span>[<a href="'.$this->href.''.$_GET['page'].'">下頁</a>] </span>';
        }

        $_GET['page'] = $this->countPages;
        $pageStr.='<span>[<a href="'.$this->href.''.$_GET['page'].'">尾頁</a>] </span>';

        return $pageStr;
    }

    /**
     * construct_Pages 生成頁碼數組
     * 鍵為頁碼,值為鏈接
     * $this->page_arr=Array(
     *                  [1] => index.php?page=1
     *                  [2] => index.php?page=2
     *                  [3] => index.php?page=3
     *                  ......)
     */
    protected function construct_Pages(){
        //計算總頁數
        $this->countPages=ceil($this->count/$this->subPages);
        //根據當前頁計算前后頁數
        $leftPage_num=floor($this->showPages/2);
        $rightPage_num=$this->showPages-$leftPage_num;

        //左邊顯示數為當前頁減左邊該顯示的數 例如總顯示7頁 當前頁是5  左邊最小為5-3  右邊為5+3
        $left=$this->currPage-$leftPage_num;
        $left=max($left,1); //左邊最小不能小於1
        $right=$left+$this->showPages-1; //左邊加顯示頁數減1就是右邊顯示數
        $right=min($right,$this->countPages);  //右邊最大不能大於總頁數
        $left=max($right-$this->showPages+1,1); //確定右邊再計算左邊,必須二次計算

        for ($i=$left; $i <= $right; $i++) {
            $_GET['page'] = $i;
            $this->page_arr[$i]=$this->href.''.$_GET['page'];
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM