<?php$page=newPage(53,10);$p=$page->rendor();echo'<pre>';var_dump($p);echo'</pre>';/***分页类*/classPage{protected$url;//URLprotected$pageCount;//总页数protected$total;//总条数protected$num;//每页显示数protected$page;//当前页//初始化成员属性publicfunction__construct($total,$num=5){//总条数$this->total=($total>0)?(int)$total:1;//每页显示条数$this->num=$num;//总页数$this->pageCount=$this->getPageCount();//当前页$this->page=$this->getCurrentPage();//URL$this->url=$this->getUrl();}//一次性返回所有的分页信息publicfunctionrendor(){return['first'=>$this->first(),'next'=>$this->next(),'prev'=>$this->prev(),'end'=>$this->end(),];}//limit方法,在未来分页数据查询的时候,直接返回对应的limit0,5这样的字符串publicfunctionlimit(){$offset=($this->page-1)*$this->num;$str=$offset.','.$this->num;return$str;}//首页,设置page=1protectedfunctionfirst(){return$this->setQueryString('page=1');}//上一页protectedfunctionprev(){$page=($this->page<=1)?1:($this->page-1);return$this->setQueryString('page='.$page);}//下一页protectedfunctionnext(){$page=($this->page>=$this->pageCount)?$this->pageCount:($this->page+1);return$this->setQueryString('page='.$page);}//首页protectedfunctionend(){return$this->setQueryString('page='.$this->pageCount);}//一种情况有?一种情况没有?protectedfunctionsetQueryString($page){//查找url中是否有问号if(stripos($this->url,'?')){return$this->url.'&'.$page;}else{//没有问号就拼接return$this->url.'?'.$page;}}//处理URLprotectedfunctiongetUrl(){//获取用户的uri$path=$_SERVER['REQUEST_URI'];//解析uri$par=parse_url($path);//判断用户是否设置过queryif(isset($par['query'])){parse_str($par['query'],$query);//检查query里面时候有page,如果有的话就干掉pageunset($query['page']);$path=$par['path'].'?'.http_build_query($query);}$path=rtrim($path,'?');//协议:主机:端口:文件和请求//判断是否定义过端口,并且端口是否为443,如果为443则是https协议,否则就是http协议$protocal=(isset($_SERVER['SERVER_PORT'])&&$_SERVER['SERVER_PORT']==443)?'https//':'http://';if(80==$_SERVER['SERVER_PORT']||443==$_SERVER['SERVER_PORT']){$url=$protocal.$_SERVER['SERVER_NAME'].$path;}else{$url=$protocal.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$path;}//全新的urlreturn$url;}//处理当前页PageprotectedfunctiongetCurrentPage(){//如果有page,就返回转换为int的pageif(isset($_GET['page'])){//得到页码$page=(int)$_GET['page'];//当前页码不能给大于总页码if($page>$this->pageCount){$page=$this->pageCount;}if($page<1){$page=1;}}else{$page=1;}return$page;}//处理总页数protectedfunctiongetPageCount(){//进一法取整returnceil($this->total/$this->num);}}