修改过的Page.class.php,支持select跳转

jerry PHP 2015年11月18日 收藏
感觉THinkPHP原版的不太好使,Page.class.php,自己修改了下可以自定义需要那些组件
感觉THinkPHP原版的不太好使,Page.class.php,自己修改了下可以自定义需要那些组件 ,话说配图是必须的

自己的网站也是必须带的:http://sssui.com
不要在意这些细节
下面是代码
  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.     // 分页URL地址
  19.     public $url     =   '';
  20.     // 默认列表每页显示行数
  21.     public $listRows = 20;
  22.     // 起始行数
  23.     public $firstRow    ;
  24.     // 分页总页面数
  25.     protected $totalPages  ;
  26.     // 总行数
  27.     protected $totalRows  ;
  28.     // 当前页数
  29.     protected $nowPage    ;
  30.     // 分页的栏的总页数
  31.     protected $coolPages   ;
  32.     // 分页显示定制
  33.     protected $config  =    array(
  34.         'header'=>'条记录',
  35.         'prev'=>'上一页',
  36.         'next'=>'下一页',
  37.         'first'=>'第一页',
  38.         'last'=>'最后一页',
  39.         'widget'=>array(
  40.             'UpDown'  =>true,
  41.             'PreNext'  =>false,
  42.             'FirstEnd'  =>true,
  43.             'PageLink'  =>true,
  44.             'JumpPage'  =>true,
  45.         ),
  46.         'theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %first% %prePage% %upPage% %linkPage% %downPage% %nextPage% %end% %jumppage%'
  47.     );
  48.     // 默认分页变量名
  49.     protected $varPage;

  50.     /**
  51.      * 架构函数
  52.      * @access public
  53.      * @param array $totalRows  总的记录数
  54.      * @param array $listRows  每页显示记录数
  55.      * @param array $parameter  分页跳转的参数
  56.      */
  57.     public function __construct($totalRows,$listRows='',$parameter='',$url='') {
  58.         $this->totalRows    =   $totalRows;
  59.         $this->parameter    =   $parameter;
  60.         $this->varPage      =   C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
  61.         if(!empty($listRows)) {
  62.             $this->listRows =   intval($listRows);
  63.         }
  64.         $this->totalPages   =   ceil($this->totalRows/$this->listRows);     //总页数
  65.         $this->coolPages    =   ceil($this->totalPages/$this->rollPage);
  66.         $this->nowPage      =   !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
  67.         if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
  68.             $this->nowPage  =   $this->totalPages;
  69.         }
  70.         $this->firstRow     =   $this->listRows*($this->nowPage-1);
  71.     }

  72.     public function setConfig($name,$value) {
  73.         if(isset($this->config[$name])) {
  74.             $this->config[$name]    =   $value;
  75.         }
  76.     }

  77.     /**
  78.      * 分页显示输出
  79.      * @access public
  80.      */
  81.     public function show() {
  82.         if(0 == $this->totalRows) return '';
  83.         $p              =   $this->varPage;
  84.         $nowCoolPage    =   ceil($this->nowPage/$this->rollPage);
  85.         // 分析分页参数
  86.         if($this->url){
  87.             $depr       =   C('URL_PATHINFO_DEPR');
  88.             $url        =   rtrim(U('/'.$this->url,'',false),$depr).$depr.'__PAGE__';
  89.         }else{
  90.             if($this->parameter && is_string($this->parameter)) {
  91.                 parse_str($this->parameter,$parameter);
  92.             }elseif(empty($this->parameter)){
  93.                 unset($_GET[C('VAR_URL_PARAMS')]);
  94.                 if(empty($_GET)) {
  95.                     $parameter  =   array();
  96.                 }else{
  97.                     $parameter  =   $_GET;
  98.                 }
  99.             }
  100.             $parameter[$p]  =   '__PAGE__';
  101.             $url            =   U('',$parameter);
  102.         }
  103.         //上下翻页字符串
  104.         if($this->config['widget']['UpDown']){
  105.             $upRow          =   $this->nowPage-1;
  106.             $downRow        =   $this->nowPage+1;
  107.             if ($upRow>0){
  108.                 $upPage     =   "<a href='".str_replace('__PAGE__',$upRow,$url)."'>".$this->config['prev']."</a>";
  109.             }else{
  110.                 $upPage     =   '';
  111.             }

  112.             if ($downRow <= $this->totalPages){
  113.                 $downPage   =   "<a href='".str_replace('__PAGE__',$downRow,$url)."'>".$this->config['next']."</a>";
  114.             }else{
  115.                 $downPage   =   '';
  116.             }
  117.         }
  118.         // << < > >>
  119.         if($this->config['widget']['PreNext']){
  120.             if($nowCoolPage == 1){
  121.                 $prePage    =   '';
  122.             }else{
  123.                 $preRow     =   $this->nowPage-$this->rollPage;
  124.                 $prePage    =   "<a href='".str_replace('__PAGE__',$preRow,$url)."' >上".$this->rollPage."页</a>";
  125.             }
  126.             if($nowCoolPage == $this->coolPages){
  127.                 $nextPage   =   '';
  128.             }else{
  129.                 $theEndRow  =   $this->totalPages;
  130.                 $nextPage   =   "<a href='".str_replace('__PAGE__',$nextRow,$url)."' >下".$this->rollPage."页</a>";
  131.             }
  132.         }
  133.         if($this->config['widget']['FirstEnd']){
  134.             if($this->nowPage == 1){
  135.                 $theFirst   =   '';
  136.             }else{
  137.                 $theFirst   =   "<a href='".str_replace('__PAGE__',1,$url)."' >".$this->config['first']."</a>";
  138.             }
  139.             if($this->nowPage == $this->totalPages){
  140.                 if($this->config['widget']['FirstEnd']) $theEnd     =   '';
  141.             }else{
  142.                 $theEndRow  =   $this->totalPages;
  143.                 $theEnd     =   "<a href='".str_replace('__PAGE__',$theEndRow,$url)."' >".$this->config['last']."</a>";
  144.             }
  145.         }
  146.         
  147.         // 1 2 3 4 5

  148.         if($this->config['widget']['PageLink']){
  149.             $linkPage = "";
  150.             for($i=1;$i<=$this->rollPage;$i++){
  151.                 $page       =   ($nowCoolPage-1)*$this->rollPage+$i;
  152.                 if($page!=$this->nowPage){
  153.                     if($page<=$this->totalPages){
  154.                         $linkPage .= " <a href='".str_replace('__PAGE__',$page,$url)."'> ".$page." </a>";
  155.                     }else{
  156.                         break;
  157.                     }
  158.                 }else{
  159.                     if($this->totalPages != 1){
  160.                         $linkPage .= " <span class='current'>".$page."</span>";
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.         if($this->config['widget']['JumpPage']){
  166.             $jumppage='<select onchange="var PageJumpUrl=\''.$url.'\';window.location=PageJumpUrl.replace(\'__PAGE__\',this.value);">';
  167.             for ($j=1; $j <=$this->totalPages ; $j++) {
  168.                 $select= ($this->nowPage==$j) ? 'selected' : '';
  169.                 $jumppage.='<option value="'.$j.'" '.$select.'>'.$j.'</option>';
  170.             }
  171.             $jumppage.='</select>';
  172.         }
  173.         $pageStr     =   str_replace(
  174.             array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%','%jumppage%'),
  175.             array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd,$jumppage),$this->config['theme']);
  176.         return $pageStr;
  177.     }

  178. }