欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php自适应分页类

程序员文章站 2022-06-16 07:52:48
...
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->pageleft){                $this->end=$this->link;            }            //2.中间            if(($this->page>$this->left) && (($this->page+$this->right)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->pagepage=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;    }        //渲染页码         public function show(array $arr=array()){        if(!count($arr)){            $arr=$this->theme;        }        $url=$this->getUrl();        //首页、上一页约束        if($this->page==1){            $html['first']="首页";            $html['prev']="上一页";                    }else{            $html['first']="首页";            $html['prev']="上一页";                    }                $str='';        //起始点页码        if($this->start!=1){            $str='...';        }                for($i=$this->start;$iend;$i++){            if($i==$this->page){                $str.="$i";                continue;            }            $str.="$i";        }        //结束点页码        if($this->end!=$this->pages){            $str.='...';        }                $html['link']=$str;                //下一页、末页约束        if($this->page==$this->pages){            $html['next']="下一页";            $html['last']="末页";                    }else{            $html['next']="下一页";            $html['last']="末页";                    }        $html['total']="共有".$this->total."条结果 ";        $html['info']="当前为第".$this->page."/".$this->pages."页";                $res='';        foreach($arr as $v){            $res.=$html[$v];        }        return $res;    }}
相关标签: php自适应分页类