8月232014
php简单通用的分页类
<?php /** */ class Page{ protected $url; //地址 protected $allnum; //总记录数 protected $current; //当前页码 protected $pagesize; //每页显示多少条记录 protected $postfix; //后缀 protected $style; //显示样式 共3种样式,1 2 3 分别表示前5后4,前3后3,前4后4 public function __construct($url,$allnum,$current,$pagesize=10,$postfix='',$style=1){ $this->url=$url; $this->allnum=$allnum; $this->current=$current; $this->pagesize=$pagesize; $this->postfix=$postfix; $this->style=$style; } //获取总页数 protected function maxPageNum(){ $max=ceil($this->allnum/$this->pagesize); //页码超限校正 if($this->current>$max){ $this->current=$max; } if($this->current<1){ $this->current=1; } return $max; } //获得第一页链接完整html str protected function firstUrl(){ if($this->current!=1) { return '<span id="firstpage"><a href="'.$this->getUrl(1).'" title="查看第一页">首页</a></span>'; }else{ return '<span id="firstpage"><a>首页</a></span>'; } } //获得上一页链接完整html str protected function prevUrl(){ if($this->current<=1){ $fullurl='<span id="prevpage"><a>上一页</a></span>'; }else{ $fullurl=$this->url.($this->current-1).$this->postfix; $fullurl='<span id="prevpage" title="查看上一页"><a href="'.$fullurl.'">上一页</a></span>'; } return $fullurl; } //获得下一页链接完整html str protected function nextUrl(){ if($this->current>=$this->maxPageNum()){ $fullurl='<span id="nextpage"><a>下一页</a></span>'; }else{ $fullurl=$this->url.($this->current+1).$this->postfix; $fullurl='<span id="nextpage" title="查看下一页"><a href="'.$fullurl.'">下一页</a></span>'; } return $fullurl; } //获得最后一页链接完整html str protected function lastUrl(){ if($this->current>=$this->maxPageNum()){ $fullurl='<span id="lastpage"><a>末页</a></span>'; }else{ $fullurl=$this->url.$this->maxPageNum().$this->postfix; $fullurl='<span id="lastpage" title="最后一页"><a href="'.$fullurl.'">末页</a></span>'; } return $fullurl; } //获得指定页码完整url protected function getUrl($pageno){ return $this->url.$pageno.$this->postfix; } //指定显示样式 protected function getStyle(){ switch($this->style){ case 1: $before=5; $after=4; break; case 2: $before=3; $after=3; break; case 3: $before=4; $after=4; break; default : $before=5; $after=4; } return array($before,$after); } //获得中间URL 1 2 3 4 5 ⑥ 7 8 9 10 protected function getMiddelUrl(){ $max=$this->maxPageNum(); //先获取总页,可校正当前页超限问题 $current=$this->current; //当前页码必须合法,才能保证下面的页码范围一定正确 //得到当前样式 list($before,$after)=$this->getStyle(); $startno=$current-$before; //起始页码 $endno=$current+$after; //终止页码 //为保证输出始终符合要求,在页面不超限前提下,起始页增加,则终止页必须增加,反之同理. while($endno>$max||$startno<1){ if($endno>$max){ //终止页码超界 $endno--; if($startno>1){ $startno--; } } if($startno<1){ //起始页码超界 $startno++; if($endno<$max){ $endno++; } } } $str=''; //用于保存整个html str for($i=$startno;$i<=$endno;$i++){ $currenturl=$this->getUrl($i); if($i!=$current){ $str.="<a href='{$currenturl}'>{$i}</a>"; }else{ $str.='<span id="current"><a>'.$i.'</a></span>'; } } return $str; } //返回完整分页html字符串 public function getPageStr(){ $str='<div id="pagelist">'.$this->firstUrl().$this->prevUrl(); $str.=$this->getMiddelUrl(); $str.=$this->nextUrl().$this->lastUrl().'<span id="allpagesize">共'.$this->maxPageNum().'页'.$this->allnum.'条</span></div>'; return $str; } } ?>
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!