page 分页类

jerry 2015年11月18日 收藏
我也是想做个自己的分页类,所以在案例里面加入了注释,如果不地方不对,请告诉我
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // |         lanfengye <zibin_5257@163.com>
  11. // +----------------------------------------------------------------------

  12. class Page {
  13.     
  14.     // 分页栏每页显示的页数
  15.     public $rollPage = 5;
  16.     // 页数跳转时要带的参数
  17.     public $parameter  ;
  18.     // 默认列表每页显示行数
  19.     public $listRows = 20;
  20.     // 起始行数
  21.     public $firstRow    ;
  22.     // 分页总页面数
  23.     protected $totalPages  ;
  24.     // 总行数
  25.     protected $totalRows  ;
  26.     // 当前页数
  27.     protected $nowPage    ;
  28.     // 分页的栏的总页数
  29.     protected $coolPages   ;
  30.     // 分页显示定制
  31.     protected $config  =    array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>'%upPage% %first%  %prePage%  %linkPage%  %nextPage% %downPage% %end%');
  32.     // protected $config  =    array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');
  33.     // 默认分页变量名
  34.     protected $varPage;

  35.     /**
  36.      * 架构函数
  37.      * @access public
  38.      * @param array $totalRows  总的记录数
  39.      * @param array $listRows  每页显示记录数
  40.      * @param array $parameter  分页跳转的参数
  41.      */
  42.     public function __construct($totalRows,$listRows='',$parameter='') {
  43.         $this->totalRows    =   $totalRows; // 构造函数参数 1,总页数
  44.         $this->parameter    =   $parameter; // 构造函数参数 3,URL 附加参数
  45.         $this->varPage      =   C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ; // 获取分页变量名,如果未定义则定义默认分页变量名
  46.         /**
  47.          * intval() 将变量转成整数类型
  48.          */
  49.         if(!empty($listRows)) { // 构造函数参数 2,获取每页显示的条数,如果每页显示的条数不为空则
  50.             $this->listRows =   intval($listRows); // 转换为整型并赋值给每页显示的条数
  51.         }
  52.         /**
  53.          * ceil() 函数向上舍入为最接近的整数(1.1=2)
  54.          */
  55.         $this->totalPages   =   ceil($this->totalRows/$this->listRows); // 获取总页数,记录集的总数除以每页显示的条数等于总页数
  56.         // 假设有 40 条数据,每页显示 5 条,就是有 8 页,每个页面显示 2 个导航栏,就是有4栏
  57.         $this->coolPages    =   ceil($this->totalPages/$this->rollPage); // 获取总栏数, 总栏数除以每页显示的栏数等于总栏数
  58.         /**
  59.          * empty() 如果参数是非空或非零的值,则返回 FALSE,否则返回 TRUE
  60.          */
  61.         $this->nowPage      =   !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1; // 获取当前页数,如果 URL 当前页数参数不为空则转换整型并赋值给当前页数,否则赋值为 1
  62.         if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) { // 如果总页数不为空并且当前页数大于总页数则
  63.             $this->nowPage  =   $this->totalPages; // 赋值当前页数为总页数
  64.         }
  65.         // 假设当前页数为 2,每页显示 5 条数据,当前页面就是从第 (5*(2-1)=5) 条记录开始读取数据,
  66.         // 根据 limit 函数定义,索引从零开始,也就是实际的值是记录集的第六条数据
  67.         $this->firstRow     =   $this->listRows*($this->nowPage-1); // 获取起始页,起始行数等于每页显示的条数乘以当前页面减 1
  68.     }
  69.     /**
  70.      * 自定义导航显示
  71.      * @access public
  72.      * @param String $name 待替换的参数名称
  73.      * @param String $value 替换的参数值
  74.      * isset() 返回  bool 值
  75.      * 若变量不存在则返回 FALSE 
  76.      * 若变量存在且其值为NULL,也返回 FALSE 
  77.      * 若变量存在且值不为NULL,则返回 TURE 
  78.      */    
  79.     public function setConfig($name,$value) {
  80.         if(isset($this->config[$name])) {
  81.             $this->config[$name]    =   $value;
  82.         }
  83.     }

  84.     /**
  85.      * 分页显示输出
  86.      * @access public
  87.      * @author lanfengye <zibin_5257@163.com>
  88.      */
  89.     public function show() {
  90.         if(0 == $this->totalRows) return '';
  91.         $p              =   $this->varPage; // 默认分页变量名
  92.         // 假设 40 条数据,每页显示 5 条,导航每页显示 4 栏,当前为第 3 页,也就是 ceil(3/4)=1
  93.         $nowCoolPage    =   ceil($this->nowPage/$this->rollPage); // 当前分页栏
  94.         
  95.         //获取控制器名和方法名,并判断是否url不区分大小写
  96.         $url_case       =   C('URL_CASE_INSENSITIVE');
  97.         $module_name    =   $url_case?  parse_name(MODULE_NAME) :   MODULE_NAME;
  98.         $action_name    =   $url_case?  parse_name(ACTION_NAME) :   ACTION_NAME;
  99.         
  100.         //替换附加参数中的分隔符
  101.         $parameter      =   str_replace(array('&','='), C('URL_PATHINFO_DEPR'), $this->parameter);

  102.         //增加附加参数
  103.         $url            =   rtrim(.'/'.$module_name.C('URL_PATHINFO_DEPR').$action_name.C('URL_PATHINFO_DEPR').$parameter,C('URL_PATHINFO_DEPR'));
  104.         
  105.         //上翻页字符串
  106.         $upRow          =   $this->nowPage-1; // 上一页等于当前页减 1
  107.         // 假设有 40 条记录,每页显示 5 条,当前页数为 1 的时候,上一页就会出现等于 0 的情况。
  108.         if ($upRow>0){ // 如果上一页大于零,输出上一页的链接,            
  109.             $upPage     =   "<a href='".$url.C('URL_PATHINFO_DEPR').$p.C('URL_PATHINFO_DEPR')."$upRow".C('URL_HTML_SUFFIX')."'>".$this->config['prev']."</a>";
  110.         }else{ // 如果上一页小于零,说明当前已经是第一页,不需要上一页链接输出
  111.             $upPage     =   '';
  112.         }
  113.         
  114.         // 下翻页字符串
  115.         $downRow        =   $this->nowPage+1; // 下一页等于当前页加 1
  116.         // 假设有 40 条记录,每页显示 5 条,当前页数为 7 的时候,下一页就会出现等于总页数的情况。
  117.         if ($downRow <= $this->totalPages){ // 如果下一页小于等于总页数,输出下一页链接
  118.             $downPage   =   "<a href='".$url.C('URL_PATHINFO_DEPR').$p.C('URL_PATHINFO_DEPR')."$downRow".C('URL_HTML_SUFFIX')."'>".$this->config['next']."</a>";
  119.         }else{ // 如果下一页小于零,说明当前已经是最后一页,不需要下一页链接输出
  120.             $downPage   =   '';
  121.         }
  122.         
  123.         // << <
  124.         if($nowCoolPage == 1){ // 如果当前分页栏数为 1 时,当前在第一栏,
  125.             $theFirst   =   '';
  126.             $prePage    =   '';
  127.         }else{ // 否则不在第一栏,输出上一栏链接
  128.             $preRow     =   $this->nowPage-$this->rollPage; // 通过当前页数减去每页显示的条页,获取上一页的链接并,并输出
  129.             $prePage    =   "<a href='".$url.C('URL_PATHINFO_DEPR').$p.C('URL_PATHINFO_DEPR')."$preRow".C('URL_HTML_SUFFIX')."' >上".$this->rollPage."页</a>";
  130.             // 输出第一页的链接
  131.             $theFirst   =   "<a href='".$url.C('URL_PATHINFO_DEPR').$p.C('URL_PATHINFO_DEPR')."1".C('URL_HTML_SUFFIX')."' >".$this->config['first']."</a>";
  132.         }
  133.         
  134.         // > >>
  135.         if($nowCoolPage == $this->coolPages){ // 如果当前栏等于最后一栏,当前栏为最后一栏
  136.             $nextPage   =   '';
  137.             $theEnd     =   '';
  138.         }else{ // 否则不在最后一栏,输出最后一栏链接
  139.             $nextRow    =   $this->nowPage+$this->rollPage; // 通过当前页数加上每页显示的条页,获取下一页的链接并,并输出
  140.             $theEndRow  =   $this->totalPages;
  141.             $nextPage   =   "<a href='".$url.C('URL_PATHINFO_DEPR').$p.C('URL_PATHINFO_DEPR')."$nextRow".C('URL_HTML_SUFFIX')."' >下".$this->rollPage."页</a>";
  142.             // 输出最后一条链接
  143.             $theEnd     =   "<a href='".$url.C('URL_PATHINFO_DEPR').$p.C('URL_PATHINFO_DEPR')."$theEndRow".C('URL_HTML_SUFFIX')."' >".$this->config['last']."</a>";
  144.         }
  145.         
  146.         // 1 2 3 4 5
  147.         $linkPage = "";
  148.         for($i=1;$i<=$this->rollPage;$i++){
  149.             // 在96行定义,假设每页显示 5 条数据,
  150.             // 当栏数为 1 时,公式等于{(1-1)*5+i},page 值等于 1、2、3、4、5
  151.             // 当栏数为 2 时,公式等于{(2-1)*5+i},page 值等于 6、7、8、9、10            
  152.             $page       =   ($nowCoolPage-1)*$this->rollPage+$i;
  153.             if($page!=$this->nowPage){ // 当 page 值不等于当前页数的时
  154.                 if($page<=$this->totalPages){ // 当 page 值小于等于总页数时,输出链接
  155.                     $linkPage .= " <a href='".$url.C('URL_PATHINFO_DEPR').$p.C('URL_PATHINFO_DEPR')."$page".C('URL_HTML_SUFFIX')."'> ".$page." </a>";
  156.                 }else{ // 否则返回
  157.                     break;
  158.                 }
  159.             }else{ // 否则并且  page 值不等于 1 输出当前页面数(无链接)
  160.                 if($this->totalPages != 1){ // ?????有没有这个判断貌似一样?????
  161.                     $linkPage .= " <span class='current'>".$page."</span>";
  162.                 }
  163.             }
  164.         }
  165.         
  166.         $pageStr     =     str_replace(
  167.             array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),
  168.             array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']);
  169.         return $pageStr;
  170.     }

  171. }