您的位置:首页技术文章
文章详情页

PHP完美分页类

浏览:2日期:2022-09-13 13:23:20

<?phpclass Page{private $total; //数据表中总记录数private $listrows; //每页显示行数private $limit;private $uri;//自动获取url请求地址private $pageNum;//总页数private $config = array(’header’=>’条记录’,’prev’=>’上一页’,’next’=>’下一页’,’first’=>’首页’,’last’=>’尾页’);public function __construct($total,$listrows=10,$pa=''){$this->total = $total;$this->listrows = $listrows;$this->uri = $this->getUri($pa);$this->page = !empty($_GET[’page’]) ? $_GET[’page’]:1 ;$this->pageNum = ceil($this->total/$this->listrows);$this->limit = $this->setLimit();}private function setLimit(){return 'limit '.($this->page-1)*$this->listrows.’,’.$this->listrows;}public function getUri($pa){ $url = $_SERVER[’REQUEST_URI’].(strpos($_SERVER[’REQUEST_URI’],’?’)?’’:'?').$pa;$parse = parse_url($url); //返回一个数组,数组有元素query和pathif(isset($parse[’query’])){parse_str($parse[’query’],$params); //将query中的参数解析到$params,为数组unset($params[’page’]);$url = $parse[’path’].’?’.http_build_query($params); //按照指定的参数生成一个请求字符串}return $url;} //limit为私有属性,通过这个魔术方法使limit变得可用private function __get($args){if($args=='limit'){return $this->limit;}else{return null;}}private function first(){if($this->page==1){$html.=’’;}else{ $html.='&nbsp;<a href=’{$this->uri}&page=1’>{$this->config[’first’]}</a>&nbsp;';}return $html;}private function prev(){if($this->page==1){$html.=’’;}else{ $html.='&nbsp;<a href=’{$this->uri}&page='.($this->page-1).'’>{$this->config[’prev’]}</a>&nbsp;';}return $html;}private function pagelist(){$linkPage=’’;//每边显示inum个页码$inum = 3;//左边的页码for($i=$inum;$i>=1;$i--){$page = $this->page-$i;if($page<1){continue;}else{$linkPage.='&nbsp;<a href=’{$this->uri}&page={$page}’>{$page}</a>&nbsp;';}}//当前页码$linkPage.='&nbsp;{$this->page}&nbsp;';//右边页码for($i=1;$i<=$inum;$i++){$page = $this->page+$i;if($page>$this->pageNum){break;}else{$linkPage.='&nbsp;<a href=’{$this->uri}&page={$page}’>{$page}</a>&nbsp;';}}return $linkPage;}private function next(){if($this->page==$this->pageNum){$html.=’’;}else{ $html.='&nbsp;<a href=’{$this->uri}&page='.($this->page+1).'’>{$this->config[’next’]}</a>&nbsp;';}return $html;}private function last(){if($this->page==$this->pageNum){$html.=’’;}else{ $html.='&nbsp;<a href=’{$this->uri}&page={$this->pageNum}’>{$this->config[’last’]}</a>&nbsp;';}return $html;}private function goPage(){return ’&nbsp;&nbsp;<input type='text' onkeydown='javascript:if(event.keyCode==13){ var page=(this.value>’.$this->pageNum.’)?’.$this->pageNum.’:this.value; location=’’.$this->uri.’&page=’+page+’’ }' value='’.$this->page.’' ><input type='button' value='GO' onclick='javascript:varpage=(document.getElementById(’in’).value>’.$this->pageNum.’)?’.$this->pageNum.’:document.getElementById(’in’).value; location=’’.$this->uri.’&page=’+page+’’ '>&nbsp;&nbsp;’;}//*可见的成员方法function fpage($arr=array(0,1,2,3,4,5,6,7)){$html[0]='&nbsp;共有<b>{$this->total}</b>{$this->config[’header’]}&nbsp;';$html[1]='&nbsp;{$this->page}/{$this->pageNum}页&nbsp;';$html[2]=$this->first();$html[3]=$this->prev();$html[4]=$this->pageList();$html[5]=$this->next();$html[6]=$this->last();$html[7]=$this->goPage();$fpage=’’;foreach($arr as $index){$fpage.=$html[$index];}return $fpage;}}

标签: PHP
相关文章: