PHP之分页类
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/6/22 * Time: 21:37 */ Class Page{ protected $sum; //总记录数 protected $page_show; //每页显示多少记录 protected $totalpage; //总页数 protected $page; //当前页面 protected $limit; protected $url; protected $start; //开始 protected $end; //结束 function __construct($sum,$row=1) { $this->sum=$sum; //总记录数 $this->page_show=$row; //每页显示 $this->totalpage=ceil($this->sum/$this->page_show);//总页数 $this->url=$this->curUrl(); //地支 $this->page=$this->curPage(); //当前页 $this->start=($this->page-1)*$this->page_show; //起始页 } //获取当前页 //min($this->totalpage,max((int)@$_GET['page'],1)); public function curPage() { if(!empty($_GET['page'])) { if($_GET['page']>$this->totalpage) { $page=$this->totalpage; }else{ $page=$_GET['page']; } }else{ $page=1; } return $page; } //获取当前地址 public function curUrl(){ // $_url=$_SERVER['REUQEST_URI']; //当前的Url // $_par=parse_url($_url); //分解当前URL // if(isset($_par['query'])) // { // // } $curUrl=isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:$_SERVER['PHP_SELF'].$_SERVER['QUERY_STRING']; //解析字符串 $parseURL=parse_url($curUrl); //分解获取查询字符串 if(isset($parseURL['query'])) { parse_str($parseURL['query'],$arr); //将字符串解析成变量,并放去数组中 unset($arr['page']); //$curUrl=$parseURL['path']."?".http_build_query($arr); $curUrl = $parseURL['path']."?".http_build_query($arr)."&page="; //var_dump($curUrl); }else{ $curUrl=strstr($curUrl,"?")?$curUrl."page=":$curUrl."?page="; } return $curUrl; } //分页查询记录数 public function limit() { return "LIMIT {$this->start},{$this->page_show}"; } //上一页 public function pre() { return $this->page>1?"<a href='".$this->url.($this->page-1)."'>上一页</a>":'1'; } //下一页 public function next(){ return $this->page<$this->totalpage?"<a href='".$this->url.($this->page+1)."'>下一页</a>":''; } //首页 public function index() { return $this->page>1?"<a href='{$this->url}1'>首页</a>":'1'; } //未接 public function end() { return $this->page<$this->totalpage?"<a href='{$this->url}{$this->totalpage}'>未页</a>":''; } //总页数 public function count() { return "<span>总页{$this->totalpage} 总计{$this->sum}条</span>"; } //页码数组 public function pagearray() { $pagelist=array(); for($i=1;$i<$this->totalpage;$i++) { if($i==$this->page){ $pagelist[$i]['url']=""; $pagelist[$i]['str']=$i; continue; } $pagelist[$i]['url']=$this->url.$i; $pagelist[$i]['str']=$i; } return $pagelist; } //下拉列表 public function select() { $arr=$this->pagearray(); $str="<select class='s' onchange='javascript:location.href=this.options[selectedIndex].value'>"; foreach($arr as $v) { $str.=empty($v['url'])?"<option value='{$this->url}{$v['str']}' selected='selected'>{$v['str']}</option>":"<option value='{$v['url']}'>{$v['str']}</option>"; } $str.="</select>"; return $str; } } $p=new Page(20); echo $p->index(); echo "</br>"; //echo $p->curUrl(); echo "</br>"; echo $p->end(); echo $p->count(); echo $p->select();