分页类
<?php class page{ protected $total; //总条数 protected $perpage = 10; //每页显示的天数 protected $count; //总页数 protected $page = 1; //当前页数 protected $uri; //url地址 protected $style = 1; //分页显示的样式 public function __construct($total,$perpage=''){ if($total == 0){return '';} //接收总条数 $this->total = $total; //没页显示条数 if($perpage != ''){ $this->perpage = $perpage; } //计算总页数 $this->count = ceil($this->total / $this->perpage); //取得url地址 $url = $_SERVER['REQUEST_URI']; //使用parse_url将url拆分成数组 $parse = parse_url($url); //判断$parse['query']是否为空 if(!isset($parse['query'])){ $parse['query']= 'page='.$this->page; } //使用parse_str函数将$parse['query']部分拆分成数组 parse_str($parse['query'],$parms); //判断page是否在数组$parms中,如果不在则添加 if(!array_key_exists('page',$parms)){ $parms['page'] = 'page='.$this->page; } //取得page的值 $parms['page'] = trim($parms['page'])+0; if($parms['page'] <=0){ $this->page=1; }else if($parms['page'] >0 && $parms['page'] <= $this->count){ $this->page = $parms['page']; }else{ $this->page = $this->count; } //拼url if(count($parms) == 1){ $this->uri = $parse['path'].'?page='; }else{ unset($parms['page']); $this->uri = $parse['path'].'?'.http_build_query($parms).'&page='; } } public function show($style=1,$num=5){ //显示样式 $this->style = $style; //上一页 if($this->page > 1){ $prev = $this->uri.($this->page-1); }else{ $prev = $this->uri.$this->page; } //下一页 if($this->page < $this->count){ $next = $this->uri.($this->page+1); }else{ $next = $this->uri.$this->page; } //首页 $toppage = $this->uri.'1'; //尾页 $endpage = $this->uri.$this->count; //1 2 3 4 5 //根据当前页码计算数字的前几页和后几页 $start = $this->page - ceil(($num-1)/2); $end = $this->page + ceil(($num-1)/2); //如果$start小于1,就将少的位数补到后面去 if($start < 1){ $end += (1-$start); $start =1; if($end > $this->count){ $end = $this->count; } } //如果$end大于总页数,就将多的位数补到前面去 if($end > $this->count){ $start -= ($end - $this->count); $end = $this->count; if($start < 1 ){ $start = 1; } } $numpage = ''; for($i = $start; $i <= $end; $i++){ if($i == $this->page){ $numpage .= '<td width="25"><div class="onsyrjshang">'.$i.'</div></td>'; }else{ $numpage .= '<td width="25"><a href="'.$this->uri.$i.'" class="syrjshang1">'.$i.'</a></td>'; } } $numpage1 = ''; for($j = $start; $j <= $end; $j++){ if($j == $this->page){ $numpage1 .= '['.$j.'] '; }else{ $numpage1 .= '<a href="'.$this->uri.$j.'">['.$j.']</a> '; } } $pageLink = ''; switch($this->style){ case 1: //样式一 //首页 上一页 下一页 尾页 if($this->page == 1){ $pageLink .= '<a href="'.$next.'">下一页</a> <a href="'.$endpage.'">尾页</a>'; }else if($this->page >1 && $this->page < $this->count){ $pageLink .= '<a href="'.$toppage.'">首页</a> <a href="'.$prev.'">上一页</a> <a href="'.$next.'">下一页</a> <a href="'.$endpage.'">尾页</a>'; }else{ $pageLink .= '<a href="'.$toppage.'">首页</a> <a href="'.$prev.'">上一页</a>'; } return $pageLink; break; case 2: if($this->page == 1 && $this->page < $this->count){ $pageLink .= '<td width="710" align="right" style="padding-right:10px">总计'.$this->total.'个记录</td>'.$numpage.'<td width="65"><a href="'.$next.'" class="syrjshang2">下一页</a></td> <td width="65"><a href="'.$endpage.'" class="syrjshang2">尾页</a></td>'; }else if($this->page >1 && $this->page < $this->count){ $pageLink .= '<td width="710" align="right" style="padding-right:10px">总计'.$this->total.'个记录</td>'. '<td width="65"><a href="'.$toppage.'" class="syrjshang2">首页</a></td> '. '<td width="65"><a href="'.$prev.'" class="syrjshang2">上一页</a></td> '.$numpage.'<td width="65"><a href="'.$next.'" class="syrjshang2">下一页</a></td> <td width="65"><a href="'.$endpage.'" class="syrjshang2">尾页</a></td>'; }else if($this->count == 1){ $pageLink .= '<td width="710" align="right" style="padding-right:10px">总计'.$this->total.'个记录</td>'.$numpage; }else{ $pageLink .= '<td width="710" align="right" style="padding-right:10px">总计'.$this->total.'个记录</td><td width="65"><a href="'.$toppage.'" class="syrjshang2">首页</a></td> <td width="65"><a href="'.$prev.'" class="syrjshang2">上一页</a></td> '.$numpage; } return $pageLink; break; case 3: if($this->page == 1 && $this->page < $this->count){ $pageLink .= '上页 |<< '.$numpage1.'<a href="'.$endpage.'"> >>|</a> <a href="'.$next.'" >下页</a>'; }else if($this->page >1 && $this->page < $this->count){ $pageLink .= '<a href="'.$prev.'">上页</a> <a href="'.$toppage.'">|<<</a> '. $numpage1.'<a href="'.$endpage.'">>>|</a> <a href="'.$next.'">下页</a>'; }else if($this->count == 1){ $pageLink .= '上页 |<< '.$numpage1.'>>| 下页';; }else{ $pageLink .= '<a href="'.$prev.'">上页</a> <a href="'.$toppage.'">|<<</a> '. $numpage1.'>>| 下页'; } return $pageLink; break; } } } //$page = new page(150,10,2); ?>
效果如下: