1. 程式人生 > >PHP面向物件之分頁類

PHP面向物件之分頁類

 <?php
    class Page
    {
        protected $num;//每頁顯示條數
        protected $total;//總記錄數
        protected $pageCount;//總頁數
        protected $current;//當前頁碼
        protected $offset;//偏移量
        protected $limit;//分頁頁碼

        /**
         * 構造方法
         * @param int  $total 總記錄數
         * @param int  $num   每頁顯示條數
         */
        public function __construct($total,$num=5)
        {
            //1.每頁顯示條數
            $this->num = $num;
            //2.總記錄數
            $this->total = $total;
            //3.總頁數
            $this->pageCount = ceil($total/$num);
            //4.偏移量
            $this->offset = ($this->current-1)*$num;
            //5.分頁頁碼
            $this->limit = "{$this->offset},{$this->num}";
            //6.初始化當前頁
            $this->current();
        }

        /**
         * 初始化當前頁
         */
        public function current(){
            $this->current = isset($_GET['page'])?$_GET['page']:'1';

            //判斷當前頁最大範圍
            if ($this->current>$this->pageCount){
                $this->current = $this->pageCount;
            }

            //判斷當前頁最小範圍
            if ($this->current<1){
                $this->current = 1;
            }
        }

        /**
         * 訪問沒許可權訪問的屬性
         * @param string $key 想訪問的屬性
         * @return float|int|string 返回對應要改變的條件
         */
        public function __get($key){
            if ($key == "limit") {
                return $this->limit;
            }
            if ($key == "offset") {
                return $this->offset;
            }
            if ($key == "current") {
                return $this->current;
            }
        }

        /**
         * 處理分頁按鈕
         * @return string 拼接好的分頁按鈕
         */
        public function show(){
            //判斷初始頁碼
            $_GET['page'] = isset($_GET['page'])?$_GET['page']:'1';

            //將$_GET值賦給上下變數
            $first = $end = $prev = $next = $_GET;
            // var_dump($prev);

            //上一頁
            //判斷上一頁範圍
            if ($this->current-1<1){
                $prev['page'] = 1;
            }else{
                $prev['page'] = $this->current-1;
            }

            //下一頁
            //判斷下一頁範圍
            if ($this->current+1>$this->pageCount) {
               $next["page"] = $this->pageCount;
            }else{
                $next['page'] = $this->current+1;
            }

            /*
            首頁
            $first['page'] = 1; 

            //尾頁
            $end['page'] = $this->pageCount;
            */

            //拼接路徑
            $url = "http://".$_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"];

            //拼接陣列url位址列字尾?傳入引數
            //http://xxx/xxx/Page.class.php?page=值
            $prev = http_build_query($prev);
            $next = http_build_query($next);
            // $first = http_build_query($first);
            // $end = http_build_query($end);

            //拼接完整路徑
            $prevpath = $url."?".$prev;
            $nextpath = $url."?".$next;
            // $firstpath = $url."?".$first;
            // $endpath = $url."?".$end;

            $str = "共有{$this->total}條記錄 共有{$this->pageCount}頁 ";
            $str .= "<a href='{$url}?page=1'>首頁</a> ";
            $str .= "<a href='{$prevpath}'>上一頁</a> ";
            $str .= "<a href='{$nextpath}'>下一頁</a> ";
            $str .= "<a href='{$url}?page={$this->pageCount}'>尾頁</a> ";
            return $str;
        }
    }
    //自行除錯
    $a = new Page(10);
    echo $a->show();
?>