1 <?php 2 class Page 3 { 4 private $maxnum; //总数据条数 5 private $num; //每页显示数条数 6 private $page;//当前页 7 private $pagenum; //总页数 8 private $url; //url 9 private $urlparam; //url参数 10 private $startpage; //当前显示的最左页, 显示页码的区间 11 private $endpage; //当前显示的最右页 12 private $str = ''; //注意成员属性变量和方法内的普通变量的区别,作用域不同的,如果用普通变量,两个方法里的字符串没办法合到一起 13 14 public function __construct($maxnum, $num) 15 { 16 $this->maxnum = $maxnum; 17 $this->num = $num; 18 $this->pagenum = ceil($this->maxnum/$this->num); 19 $this->page = isset($_GET['page']) ? $_GET['page'] : 1; 20 $this->url = $_SERVER['PHP_SELF']; //directory + filename 21 $this->urlparam(); 22 $this->checkpage(); 23 // $this->pagemax(); 24 } 25 26 public function urlparam() 27 { 28 foreach($_GET as $k=>$v){ 29 if($v != '' && $k != 'page'){ 30 $this->urlparam .= $k.'='.$v.'&'; 31 } 32 } 33 } 34 35 public function showpage() 36 { 37 // $str = ''; 38 $this->str .= '当前第'.$this->page.'页,共'.$this->pagenum.'页'; 39 $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page=1">首页</a>'; 40 $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page='.($this->page-1).'">上一页</a>'; 41 $this->pages(); 42 $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page='.($this->page+1).'">下一页</a>'; 43 $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page='.$this->pagenum.'">尾页</a>'; 44 return $this->str; 45 } 46 47 //当前显示页码的区间 48 //...11 12 13 14 15 16 ...仿百度分页效果 49 public function pages() 50 { 51 if($this->page <= 10){ 52 $this->startpage = 1; 53 }else{ 54 //通过当前页除以10,在和1拼接,比如当前页15,经过这种方法处理就得到起始页为11.减1的目的是如果当前页是30,那么把他算作21那组 55 $this->startpage = floor(($this->page-1) / 10).'1'; //这块想要拼接只能使用string类型的1 56 } 57 $this->endpage = $this->startpage + 9; //只显示10个页码 58 if($this->endpage >= $this->pagenum){ 59 $this->endpage = $this->pagenum; // 60 } 61 for($i=$this->startpage; $i<=$this->endpage; $i++){ 62 //注意这里的变量名必须和showpage()方法里的变量名相同,因为最终是和showpage里的字符串拼接 63 // 还不行,变量作用域不同,即便起相同变量名也不行。只能使用成员属性变量的方式了 64 if($this->page == $i) 65 $this->str .= '<a style="color:red;" href="'.$this->url.'?'.$this->urlparam.'page='.$i.'">'.$i.'</a> '; 66 else 67 $this->str .='<a href="'.$this->url.'?'.$this->urlparam.'page='.$i.'">'.$i.'</a>'; 68 } 69 return $this->str; //单独把这个写成一个方法就是为了维护,如果用百度分页效果就调用,不需要就不调用 70 } 71 72 public function checkpage() 73 { 74 if($this->page >= $this->pagenum){ 75 $this->page = $this->pagenum; 76 } 77 if($this->page <= 1){ 78 $this->page = 1; 79 } 80 } 81 82 public function limit() 83 { 84 $num = ($this->page-1) * $this->num; 85 $limit = $num.','.$num; 86 return $limit; 87 } 88 } 89 90 $p = new page(90, 3); 91 echo $p->showpage();
部分内容参考:https://www.cnblogs.com/liangzia/p/10438263.html
之前做的有点瑕疵,修改了一下。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 class Page 3 { 4 private $maxnum; //总数据条数 5 private $num; //每页显示数条数 6 private $page;//当前页 7 private $pagenum; //总页数 8 private $url; //url 9 private $urlparam; //url参数 10 private $startpage; //当前显示的最左页, 显示页码的区间 11 private $midpage; //当前显示页作为中间,比如13,那么左边显示8 9 10 11 12,右边显示14 15 16 17 12 private $endpage; //当前显示的最右页 13 private $str = ''; //注意成员属性变量和方法内的普通变量的区别,作用域不同的,如果用普通变量,两个方法里的字符串没办法合到一起 14 15 public function __construct($maxnum, $num) 16 { 17 $this->maxnum = $maxnum; 18 $this->num = $num; 19 $this->pagenum = ceil($this->maxnum/$this->num); 20 $this->page = isset($_GET['page']) ? $_GET['page'] : 1; 21 $this->url = $_SERVER['PHP_SELF']; //directory + filename 22 $this->urlparam(); 23 $this->checkpage(); 24 // $this->pagemax(); 25 } 26 27 public function urlparam() 28 { 29 foreach($_GET as $k=>$v){ 30 if($v != '' && $k != 'page'){ 31 $this->urlparam .= $k.'='.$v.'&'; 32 } 33 } 34 } 35 36 public function showpage() 37 { 38 // $str = ''; 39 //if else判断首页尾页变灰 40 $this->str .= '当前第'.$this->page.'页,共'.$this->pagenum.'页'; 41 if($this->page == 1){ 42 $this->str .= '<a style="color:#0f0;" href="'.$this->url.'?'.$this->urlparam.'page=1">首页</a>'; 43 $this->str .= '<a style="color:#0f0;" href="'.$this->url.'?'.$this->urlparam.'page='.($this->page-1).'">上一页</a>'; 44 }else{ 45 $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page=1">首页</a>'; 46 $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page='.($this->page-1).'">上一页</a>'; 47 } 48 $this->pages(); 49 if($this->page == $this->pagenum){ 50 $this->str .= '<a style="color:#0f0;" href="'.$this->url.'?'.$this->urlparam.'page='.($this->page+1).'">下一页</a>'; 51 $this->str .= '<a style="color:#0f0;" href="'.$this->url.'?'.$this->urlparam.'page='.$this->pagenum.'">尾页</a>'; 52 }else{ 53 $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page='.($this->page+1).'">下一页</a>'; 54 $this->str .= '<a href="'.$this->url.'?'.$this->urlparam.'page='.$this->pagenum.'">尾页</a>'; 55 } 56 return $this->str; 57 } 58 59 //当前显示页码的区间 60 //...11 12 13 14 15 16 ...仿百度分页效果 61 public function pages() 62 { 63 if($this->page <= 10){ 64 $this->startpage = 1; 65 }elseif($this->page >= ($this->pagenum-4)){ 66 //如果是到了最后10页范围内,那么 67 $this->startpage = $this->page - (9-($this->pagenum-$this->page)); //pagenum-page是补齐10页所差的页数 68 // 比如说共30页,当前28页,那么startpage就应该是28-(9-(30-28)),不然就不够10页 69 }else{ 70 //通过当前页除以10,在和1拼接,比如当前页15,经过这种方法处理就得到起始页为11.减1的目的是如果当前页是30,那么把他算作21那组 71 $this->startpage = $this->page-5; // 72 } 73 $this->endpage = $this->startpage + 9; //只显示10个页码 74 if($this->endpage >= $this->pagenum){ 75 $this->endpage = $this->pagenum; // 76 } 77 for($i=$this->startpage; $i<=$this->endpage; $i++){ 78 //注意这里的变量名必须和showpage()方法里的变量名相同,因为最终是和showpage里的字符串拼接 79 // 还不行,变量作用域不同,即便起相同变量名也不行。只能使用成员属性变量的方式了 80 if($this->page == $i) 81 $this->str .= '<a style="color:red;" href="'.$this->url.'?'.$this->urlparam.'page='.$i.'">'.$i.'</a> '; 82 else 83 $this->str .='<a href="'.$this->url.'?'.$this->urlparam.'page='.$i.'">'.$i.'</a> '; 84 } 85 return $this->str; //单独把这个写成一个方法就是为了维护,如果用百度分页效果就调用,不需要就不调用 86 } 87 88 public function checkpage() 89 { 90 if($this->page >= $this->pagenum){ 91 $this->page = $this->pagenum; 92 } 93 if($this->page <= 1){ 94 $this->page = 1; 95 } 96 } 97 98 public function limit() 99 { 100 $num = ($this->page-1) * $this->num; 101 $limit = $num.','.$num; 102 return $limit; 103 } 104 } 105 106 $p = new page(90, 3); 107 echo $p->showpage(); 108 主要修改的是pages方法,其他不变,还有showpage方法加上了首页尾页不能点击上一页下一页的效果,使用变色代替。
补充:在显示尾页的时候,如果用户点击最后一页可能显示不够10个页码,需要判断,就是下面这个多加个表达式条件(多加红色部分)
if($this->page >= ($this->pageNum - 4) && $this->page > 10){ $this->startPage = $this->page - (9-($this->pageNum - $this->page)); }
普通分页类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php //limit 参数:越过几条,查询几条 class Page{ //成员属性 public $pageNum; //每页显示条数 public $page; //当前页 public $pageMax; //最大页数 public $maxNum; //总条数 public $url=null; //当前页的地址 public $urlParam=''; //当前页传参 //成员方法 public function __construct($maxNum,$pageNum=5){ $this->maxNum=$maxNum; $this->pageNum=$pageNum; $this->page=isset($_GET['page'])?$_GET['page']:1; $this->url=$_SERVER['PHP_SELF']; $this->urlParam(); $this->pageMax(); $this->checkPage(); //$this->pageMax(); 这个放在后面就会出现逻辑错误 可怕 为什么呢? } private function urlParam(){ foreach($_GET as $k=>$v){ if($v!='' && $k!='page'){ $this->urlParam.='&'.$k.'='.$v; } } } private function pageMax(){ $this->pageMax=ceil($this->maxNum/$this->pageNum); } public function showPage(){ $str=''; $str.='当前第'.$this->page.'页 共'.$this->pageMax.'页 共'.$this->maxNum.'条 '; $str.='<a href="'.$this->url.'?page=1'.$this->urlParam.'">首页</a> '; $str.='<a href="'.$this->url.'?page='.($this->page-1).$this->urlParam.'">上一页</a> '; $str.='<a href="'.$this->url.'?page='.($this->page+1).$this->urlParam.'">下一页</a> '; $str.='<a href="'.$this->url.'?page='.$this->pageMax.$this->urlParam.'">尾页</a>'; return $str; } private function checkPage(){ if($this->page>$this->pageMax){ $this->page=$this->pageMax; } if($this->page<1){ $this->page=1; } } public function limit(){ $num=($this->page-1)*$this->pageNum; //越过的条数 $limit=$num.','.$this->pageNum; return $limit; } } $page= new Page(50,5); //echo $page->limit(); echo $page->showPage();