首頁 > 後端開發 > php教程 > php自适应分页类

php自适应分页类

WBOY
發布: 2016-06-23 13:08:59
原創
965 人瀏覽過

<?php/**@Author: ray*@E-mail: ningbo.zhang@qq.com*@Date: 2016-3-29*@Explain: 自适应分页类**/class Page {    public $total;//总记录    public $pageSize;//每页大小    public $page;//当前页    public $pages;//总页数    public $link;//显示的页码数   1 2 3 4 5 6    public $left;//显示页码的左边数目    public $right;//显示页码的右边数目    public $pageId;//传值的变量名    public $prev;//上一页    public $next;//下一页    public $GP;//传值的方式  GET/POST    public $start;//中间显示页码的开始页码数    public $end;//中间显示页码的结束页码数        //渲染主题    public $theme=array('first','prev','link','next','last','total','info');        //构造方法    public function __construct($num, $pageSize, $link=7, $pageId='page', $GP='G'){        $this->total=$num;        $this->pageSize=$pageSize;        $this->link=$link;        $this->pageId=$pageId;        $this->GP=$GP;                $this->rule();    }            //计算页码    public function rule(){        //计算总页数        $this->pages=ceil($this->total/$this->pageSize);                //获得当前页        if($this->GP=='G'){//GET            $this->page=isset($_GET[$this->pageId])?$_GET[$this->pageId]:1;        }else{//POST            $this->page=isset($_POST[$this->pageId])?$_POST[$this->pageId]:1;        }        //约束当前页边界        $this->constraint();                //上下页        $this->prev=$this->page-1;        $this->next=$this->page+1;                //计算中间页码的开始和结束的页码数        //计算左右显示的页码数        if($this->link%2){//link为奇数            $this->left=$this->right=($this->link-1)/2;        }else{//link为偶数            $this->left=$this->link/2;            $this->right=$this->link-$this->left-1;        }        //初始化        $this->start=1;        $this->end=$this->pages;                if($this->pages>$this->link){            //1.起始页为1            if($this->page<=$this->left){                $this->end=$this->link;            }            //2.中间            if(($this->page>$this->left) && (($this->page+$this->right)<$this->pages)){                $this->start=$this->page-$this->left+1;                $this->end=$this->page+$this->right-1;            }            //3.结束页为pages            if(($this->page+$this->right)>=$this->pages){                $this->start=$this->pages-$this->link+1;            }        }            }            //边界约束    public function constraint(){        if($this->page<1) $this->page=1;        if($this->page>$this->pages) $this->page=$this->pages;    }        //获取当前页面的url    public function getUrl(){        return "http://".$_SERVER['HTTP_HOST'].($_SERVER['SERVER_PORT']!='80'?(':'.$_SERVER['SERVER_PORT']):'').$_SERVER['PHP_SELF'];    }        //获取limit    public function getLimit(){        return ($this->page-1)*$this->pageSize;    }        //改变主题    public function changeTheme(array $theme){        $this->theme=$theme;    }        //渲染页码   <a>  <span name='now'>    public function show(array $arr=array()){        if(!count($arr)){            $arr=$this->theme;        }        $url=$this->getUrl();        //首页、上一页约束        if($this->page==1){            $html['first']="<span>首页</span>";            $html['prev']="<span>上一页</span>";                    }else{            $html['first']="<a href='".$url."?".$this->pageId."=1'>首页</a>";            $html['prev']="<a href='".$url."?".$this->pageId."=".$this->prev."'>上一页</a>";                    }                $str='';        //起始点页码        if($this->start!=1){            $str='<span>...</span>';        }                for($i=$this->start;$i<=$this->end;$i++){            if($i==$this->page){                $str.="<span name='now'>$i</span>";                continue;            }            $str.="<a href='".$url."?".$this->pageId."=".$i."'>$i</a>";        }        //结束点页码        if($this->end!=$this->pages){            $str.='<span>...</span>';        }                $html['link']=$str;                //下一页、末页约束        if($this->page==$this->pages){            $html['next']="<span>下一页</span>";            $html['last']="<span>末页</span>";                    }else{            $html['next']="<a href='".$url."?".$this->pageId."=".$this->next."'>下一页</a>";            $html['last']="<a href='".$url."?".$this->pageId."=".$this->pages."'>末页</a>";                    }        $html['total']="共有".$this->total."条结果 ";        $html['info']="当前为第".$this->page."/".$this->pages."页";                $res='';        foreach($arr as $v){            $res.=$html[$v];        }        return $res;    }}
登入後複製


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板