一個很好用的php分頁類

來源:互聯網
上載者:User
class Page {    // 分頁欄每頁顯示的頁數    public $rollPage = 6;    // 頁數跳轉時要帶的參數    public $parameter  ;    // 預設列表每頁顯示行數    public $listRows = 20;    // 起始行數    public $firstRow;    // 分頁總頁面數    protected $totalPages  ;    // 總行數    protected $totalRows  ;    // 當前頁數    protected $nowPage    ;    // 分頁的欄的總頁數    protected $coolPages   ;    // 分頁顯示定製    protected $config  = array(    'redirect'=>false,    'header'=>'條記錄',    'prev'=>'上一頁',    'next'=>'下一頁',    'first'=>'1',    'last'=>'最後一頁',    'theme'=>' <div class="part1">%upPage% %first%  %prePage%  %linkPage%  %nextPage% %downPage%</div> <div class="part2">共  %totalPage% 頁');    // 預設分頁變數名    protected $varPage;    /**     +----------------------------------------------------------     * 架構函數     +----------------------------------------------------------     * @access public     +----------------------------------------------------------     * @param array $totalRows  總的記錄數     * @param array $listRows  每頁顯示記錄數     * @param array $parameter  分頁跳轉的參數     +----------------------------------------------------------     */    public function __construct($totalRows,$listRows='',$parameter='') {        $this->totalRows = $totalRows;        $this->parameter = $parameter;        $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;        if(!empty($listRows)) {            $this->listRows = intval($listRows);        }        $this->totalPages = ceil($this->totalRows/$this->listRows);     //總頁數        $this->coolPages  = ceil($this->totalPages/$this->rollPage);        //$_GET驗證        $this->nowPage = intval($_GET[$this->varPage]);        $this->nowPage = $this->nowPage > 0 ? $this->nowPage : 1;                if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {            $this->nowPage = $this->totalPages;        }        $this->firstRow = $this->listRows*($this->nowPage-1);    }    public function setConfig($name,$value) {        if(isset($this->config[$name])) {            $this->config[$name]    =   $value;        }    }    /**     +----------------------------------------------------------     * 分頁顯示輸出     +----------------------------------------------------------     * @access public     +----------------------------------------------------------     */    public function show() {        if(0 == $this->totalRows) return '';        //處理參數        $p = $this->varPage;        $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;        $parse = parse_url($url);        if(isset($parse['query'])) {            parse_str($parse['query'],$params);            unset($params[$p]);            $url   =  $parse['path'].'?'.http_build_query($params);        }                /* 分頁邏輯  */                       //當總數小於顯示的頁碼數        if ($this->totalPages <= $this->rollPage) {        $start = 1;        $end   = $this->totalPages;        }        else{        //        if  ($this->nowPage <= $this->rollPage - 1) {        $start = 1;        $end   = $this->rollPage;                $islast = true;        }          else if ($this->nowPage > $this->totalPages - $this->rollPage + 1) {        $start = $this->totalPages - ($this->rollPage - 1);        $end   = $this->totalPages;                $isfirst = true;        }        else{        //浮動數        $size = floor($this->rollPage / 2);                $start = $this->nowPage - $size;        $end   = $this->nowPage + $size;                $isfirst = true;        $islast = true;        }        }                //上下翻頁字串        $upRow   = $this->nowPage - 1;        $downRow = $this->nowPage + 1;                        /* 拼裝HTML */                //< 1...     ...last >        if ($isfirst){        $theFirst = "<a class='firstPage' href='".$url."&".$p."=1' >".$this->config['first']."</a>";        }        if ($islast){        $theEnd = "<a class='lastPage' href='".$url."&".$p."=$this->totalPages' >".$this->config['last']."</a>";        }                if ($upRow > 0){        $upPage = "<a class='upPage' href='".$url."&".$p."=$upRow'>".$this->config['prev']."</a>";        }                if ($downRow <= $this->totalPages){        $downPage = "<a class='downPage' href='".$url."&".$p."=$downRow'>".$this->config['next']."</a>";        }               if($start==3){        $linkPage .= "<a href='".$url."&".$p."=2'>2</a>";        }        if($start>=4){        $linkPage .= "<a href='".$url."&".$p."=2'>2</a> <span class='noEndClass'>...</span>";        }        //1 2 3 4 5        for($i=$start;$i<=$end;$i++){        if($i!=$this->nowPage){        $linkPage .= " <a href='".$url."&".$p."=$i'> ".$i." </a>";        }else{        $linkPage .= " <span class='current'>".$i."</span>";        }        if($i==$end){        if($i<$this->totalRows){        $linkPage .= " <span class='noEndClass'>...</span>";        }        }        }                        $pageStr = str_replace(            array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%first%','%prePage%','%linkPage%','%nextPage%','%downPage%','%end%'),            array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$theFirst,$prePage,$linkPage,$nextPage,$downPage,$theEnd),$this->config['theme']);        //顯示模式  普通false 帶跳轉ture        if (!empty($this->config['redirect'])){        $html = $pageStr;                }else{        //傳遞參數        if($this->totalPages > 1){        $redirect = " 到第 <form method='get' action=''><input name=".$p." type='text' class='page_text' size='3' maxlength='3' value='" . $this->nowPage ."'/> 頁 <input type='submit' class='page_btn' value='確定' />";        if ($params){        foreach($params as $k => $v){        $string .= "<input type='hidden' name='" . $k . "' value='" . $v . "'/>";        }        $redirect = $redirect . $string . '</form></div>';        }else{        $redirect = $redirect . '</form></div>';        }        }        //產生Html字串        $html = $pageStr . $redirect;               }          return $html;    }}

聯繫我們

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