1. 云栖社区>
  2. PHP教程>
  3. 正文

结合 thinkPHP 分页写成自己分页类

作者:用户 来源:互联网 时间:2017-12-01 17:53:48

结合 thinkPHP 分页写成自己分页类 - 摘要: 本文讲的是结合 thinkPHP 分页写成自己分页类, 1 <?php 2 // +---------------------------------------------------------------------- 3 // | 参考 ThinkPHP 分页类改变而

  1 <?php   2 // +----------------------------------------------------------------------  3 // | 参考  ThinkPHP 分页类改变而来  4 // +----------------------------------------------------------------------  5 // | 有两种样式:1、当面页码始终在中间。 2、当点到分页条两边的页码时,自动翻页(默认)。  6 // +----------------------------------------------------------------------  7 // | Author: zbseoag <[email protected]>  8 // +----------------------------------------------------------------------  9 // $Id: page.class.php  2013-05-26 00:08:49  10  11 session_start(); //由于要记录用户是向前,还是向后翻页,故启用了session 12  13 class Page{ 14  15     public $page; //当前页 16     public $nextPage; //上一页 17     public $prevPage; //下一页 18     public $pageMax; //最大页 19     public $startPage; //分页条起码 20     public $endPage;  // 分页条止码 21     public $style; //样式:$style = mid 22     public $parameter;//页数跳转时要带的参数 23     protected $varPage;//默认分页变量名 24     protected $config = array( 25         'header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'首页','last'=>'尾页', 26         'theme' => '%records% %header% %page%/%pageMax% 页  %firstPage%%prevPage%%linkPage%%nextPage%%lastPage%' 27     ); 28      29     /** 30      +---------------------------------------------------------- 31      * 架造函数 32      +---------------------------------------------------------- 33      * @param  $records   总的记录数 34      * @param  $pageSize  每页显示记录数 35      * @param  $parameter 分页跳转的参数 36      +---------------------------------------------------------- 37      */ 38      39     function __construct($records, $pageSize, $pageVal = 'p', $parameter = null){ 40          41         $this->records = $records; 42         $this->pageMax = ceil($records/$pageSize); 43         $this->page= max(intval($_GET[$pageVal]), 1); //最小为1 44         $this->page = min($this->page, $this->pageMax); //最大为末页 45         $this->nextPage  = $this->page + 1; 46         $this->prevPage   = $this->page - 1; 47         $this->varPage = $pageVal; //默认分页变量名 48         $this->parameter = $parameter; 49     } 50      51     /** 52      +---------------------------------------------------------- 53      * 设置样式 54      +---------------------------------------------------------- 55      * @param  $name   内置参数名 56      * @param  $value  给定参数值 57      +---------------------------------------------------------- 58      */ 59     function setConfig($name, $value){ 60         if(isset($this->config[$name])){ 61             $this->config[$name] = $value; 62         } 63     } 64      65     /** 66      +---------------------------------------------------------- 67      * 分页显示输出 68      +---------------------------------------------------------- 69      * @param  $length 分页条长度 70      *---------------------------------------------------------- 71      * @param  $hide 是否适时隐藏上、下一页,比如:当前是第一页,则隐藏上一页 72      +---------------------------------------------------------- 73      */ 74     function show($length, $hide = false){ 75  76         if(0 == $this->records) return ''; 77  78         $url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter; 79         $parse = parse_url($url); 80         if(isset($parse['query'])){ 81             parse_str($parse['query'],$params); 82             unset($params[$this->varPage]); 83             $url = $parse['path'].'?'.http_build_query($params); 84         } 85  86         if('mid' == $this->style){ //当前页居中 87             if($length % 2 != 0 ){ 88                 $midder = ceil($length / 2); 89                 $big_repair = $midder - 1 ;//当上面以进一法取整,则这里为减1,反之为加1 90             }else{ 91                 $big_repair = $midder = $length / 2; 92             } 93             $sml_repair = $midder- 1;  //这是小补丁 94             $this->startPage = ($this->page  + $midder) > $this->pageMax ? $this->pageMax  - $length : $this->page - $sml_repair; 95             $this->endPage = ($this->page < $midder)? $length : $this->page + $big_repair; 96         }else{ //最后一页翻页 97             $this->startPage = ($this->page >= $length)? $_SESSION['startPage'] : 1; 98             $this->endPage  = $this->startPage + $length - 1; 99             100             $this->lastPage =  $_SESSION['lastPage']; //上次的页码101             $_SESSION['lastPage'] = $this->page;  //保存当前页102             103             if($this->lastPage < $this->page){ //后翻页104                 if($this->page >= $this->endPage){105                     $this->startPage = $_SESSION['startPage'] = $this->page;106                     $this->endPage = $this->startPage + $length -1;107                 }108             }elseif($this->lastPage == $this->page){//切换109                 if($this->page == $this->endPage){110                     $this->startPage = $_SESSION['startPage'] = $this->page;111                     $this->endPage = $this->startPage + $length -1;112                 }else{113                     $this->startPage = $_SESSION['startPage'] = $this->page - $length + 1;114                     $this->endPage = $this->startPage + $length -1;115                 }116             }else{//前翻页117                 if($this->page == $this->startPage ){118                     $this->startPage = $_SESSION['startPage'] = $this->page - $length + 1;119                     $this->endPage = $this->startPage + $length -1;120                 }121             }122             //echo '开始页码:'. $this->startPage.'<br/>';123             //echo '上次页码:'. $this->lastPage.'<br/>';124         }125         //修正一下126         $this->startPage = min($this->pageMax - $length + 1, $this->startPage); //最大127         $this->startPage = max($this->startPage, 1);//最小为第一页(先求最大,再求最小)128             129         $this->endPage = $this->startPage + $length -1;130         $this->endPage = min($this->pageMax, $this->endPage); //最大为末页131         132         $_SESSION['startPage'] = $this->startPage; //修正之后,重新保存133             134         //echo '开始页码:'. $this->startPage.'<br/>';135         //echo '上次页码:'. $this->lastPage.'<br/>';136         137         $firstPage = "<a href='$url&$this->varPage=1'>".$this->config['first'].'</a>';138         $prevPage = "<a href='$url&$this->varPage=$this->prevPage'>".$this->config['prev'].'</a>';139         $nextPage = "<a href='$url&$this->varPage=$this->nextPage'>".$this->config['next'].'</a>';140         $lastPage = "<a href='$url&$this->varPage=$this->pageMax'>".$this->config['last'].'</a>';141         if($hide){ //如果是隐藏模式142             ($this->nextPage >= $this->pageMax)? $nextPage = '' : '';143             ($this->prevPage < 1)? $prevPage = '' : '';144         }145         146         for($i = $this->startPage; $i <= $this->endPage; $i++){147             if ($this->page == $i)148                 $linkPage .= "<span class='current' href='$url&$this->varPage=$i'>$i</span>";149             else150                 $linkPage .= "<a href='$url&$this->varPage=$i'>$i</a>";151         }152         $search = array('%records%','%header%','%page%','%pageMax%','%firstPage%','%prevPage%','%linkPage%','%nextPage%','%lastPage%');153 $replace = array($this->records,$this->config['header'],$this->page,$this->pageMax,$firstPage,$prevPage,$linkPage,$nextPage,$lastPage);154         $pageStr = str_replace($search, $replace, $this->config['theme']);155         $pageStr = str_replace($search,'',$pageStr); //再替换一次156         return $pageStr;157     }158     159 }160 161 $page = new Page(340, 10); 162 $theme = '%records% %header% %page%/%pageMax% 页  %firstPage%%prevPage%%linkPage%%nextPage%%lastPage%';163 $page->setConfig('theme',$theme); //可以修改样式,比如:当不要末页是,可以删除 %lastPage%,或者改变位置次序164 $page->setConfig('last','最后一页'); //可自定义文字 : 'header'='条记录','prev'='上一页','next'='下一页','first'='首页','last'='尾页'165 166 echo $page->show(10, true);167 168 ?>169 <style>170 a,span{ margin:2px 5px;}171 .current{ color:red;}172 </style>

 

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。