thinkphp增加每页显示条数的方法

 4189

需求是多加一个类似phpmyadmin一样的每页显示条数,在网上查了好久都没找到,看到thinkphp分页类是html拼接的,很low,但是方便了我修改没,就在原生分页类基础上新定义了一个num变量,show方法返回的时候 thinkphp拼接html的地方,新加了一段选择条数的代码,如下:

  1. return "<ul class='am-pagination am-pagination-right'>{$page_str}</ul>
  2.         <div class='am-dropdown  am-dropdown-up' data-am-dropdown>
  3.             <button class='am-btn am-btn-primary am-dropdown-toggle' data-am-dropdown-toggle>显示条数 <span class='am-icon-caret-up'></span></button>
  4.             <ul class='am-dropdown-content'>
  5.                 <li><a href='".$this->urlNum(10,1)."'>10</a></li>
  6.                 <li><a href='".$this->urlNum(30,1)."'>30</a></li>
  7.                 <li><a href='".$this->urlNum(50,1)."'>50</a></li>
  8.             </ul>
  9.         </div>";

然后新加的urlNum方法是这样:

  1. private function urlNum($num,$page){
  2.     $str = str_replace(urlencode('[PAGE]'), $page, $this->url);
  3.     return str_replace(urlencode('[NUM]'), $num,  $str);
  4. }

开始的时候由于page这个变量 thinkphp会先变一个转码的,后面才替换;而且page=1的时候 url里是不显示的,但是还有这个参数,导致num这个变量老是搞得url 很不稳定、经常叠加。

后面只有做了一个小牺牲(选定每页显示条数的时候 url page即使为1 也会加上),不过这并没有什么影响。

整个代码分页类 就是这样:

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
  10. // +----------------------------------------------------------------------
  11. namespace Think; 
  12. class Page{
  13.     public $firstRow; // 起始行数
  14.     public $listRows; // 列表每页显示行数
  15.     public $parameter; // 分页跳转时要带的参数
  16.     public $totalRows; // 总行数
  17.     public $totalPages; // 分页总页面数
  18.     public $rollPage   = 11;// 分页栏每页显示的页数
  19.     public $lastSuffix = true; // 最后一页是否显示总页数
  20.     private $p       = 'p'; //分页参数名
  21.     private $num       = 'num'; //分页参数名
  22.     private $url     = ''; //当前链接URL
  23.     private $nowPage = 1;
  24.     // 分页显示定制
  25.     private $config  = array(
  26.         'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
  27.         'prev'   => '&laquo;',
  28.         'next'   => '&raquo;',
  29.         'first'  => '1...',
  30.         'last'   => '...%TOTAL_PAGE%',
  31.         'theme'  => '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
  32.     ); 
  33.     /**
  34.      * 架构函数
  35.      * @param array $totalRows  总的记录数
  36.      * @param array $listRows  每页显示记录数
  37.      * @param array $parameter  分页跳转的参数
  38.      */
  39.     public function __construct($totalRows, $listRows=20, $parameter = array()) {
  40.         C('VAR_PAGE') && $this->= C('VAR_PAGE'); //设置分页参数名称
  41.         /* 基础设置 */
  42.         $this->totalRows  = $totalRows; //设置总记录数
  43.         $this->listRows   = $listRows;  //设置每页显示行数
  44.         $this->parameter  = empty($parameter) ? $_GET : $parameter;
  45.         $this->nowPage    = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
  46.         $this->nowPage    = $this->nowPage>0 ? $this->nowPage : 1;
  47.         $this->firstRow   = $this->listRows * ($this->nowPage - 1);
  48.     } 
  49.     /**
  50.      * 定制分页链接设置
  51.      * @param string $name  设置名称
  52.      * @param string $value 设置值
  53.      */
  54.     public function setConfig($name,$value) {
  55.         if(isset($this->config[$name])) {
  56.             $this->config[$name] = $value;
  57.         }
  58.     } 
  59.     /**
  60.      * 生成链接URL
  61.      * @param  integer $page 页码
  62.      * @return string
  63.      */
  64.     private function url($page){
  65.         // return str_replace(urlencode('[PAGE]'), $page, $this->url);
  66.         $num = $_GET['num'] ? $_GET['num'] : '10';
  67.         $str = str_replace(urlencode('[NUM]'), $num,   $this->url);
  68.         return str_replace(urlencode('[PAGE]'), $page, $str);
  69.     }     
  70.     private function urlNum($num,$page){
  71.         $str = str_replace(urlencode('[PAGE]'), $page, $this->url);
  72.         return str_replace(urlencode('[NUM]'), $num,  $str);
  73.     }
  74.     /**
  75.      * 组装分页链接
  76.      * @return string
  77.      */
  78.     public function show() {
  79.         if(0 == $this->totalRows) return ''; 
  80.         /* 生成URL */
  81.         // echo $this->num;die;
  82.         $this->parameter[$this->p] = '[PAGE]'; 
  83.         // $num = empty($_GET['num']) ? '20' : '';
  84.         $this->parameter[$this->num] = '[NUM]';
  85.         $this->url = U(ACTION_NAME, $this->parameter);
  86.         /* 计算分页信息 */
  87.         $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
  88.         if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
  89.             $this->nowPage = $this->totalPages;
  90.         } 
  91.         /* 计算分页临时变量 */
  92.         $now_cool_page      = $this->rollPage/2;
  93.         $now_cool_page_ceil = ceil($now_cool_page);
  94.         $this->lastSuffix && $this->config['last'] = $this->totalPages; 
  95.         //上一页
  96.         $up_row  = $this->nowPage - 1;
  97.         $up_page = $up_row > 0 ? '<li><a class="prev" href="' . $this->url($up_row) . '">' . $this->config['prev'] . '</a></li>' : ''; 
  98.         //下一页
  99.         $down_row  = $this->nowPage + 1;
  100.         $down_page = ($down_row <= $this->totalPages) ? '<li><a class="next" href="' . $this->url($down_row) . '">' . $this->config['next'] . '</a></li>' : ''; 
  101.         //第一页
  102.         $the_first = '';
  103.         if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
  104.             $the_first = '<li><a class="first" href="' . $this->url(1) . '">' . $this->config['first'] . '</a></li>';
  105.         } 
  106.         //最后一页
  107.         $the_end = '';
  108.         if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
  109.             $the_end = '<li><a class="end" href="' . $this->url($this->totalPages) . '">' . $this->config['last'] . '</a></li>';
  110.         } 
  111.         //数字连接
  112.         $link_page = "";
  113.         for($i = 1; $i <= $this->rollPage; $i++){
  114.             if(($this->nowPage - $now_cool_page) <= 0 ){
  115.                 $page = $i;
  116.             }elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
  117.                 $page = $this->totalPages - $this->rollPage + $i;
  118.             }else{
  119.                 $page = $this->nowPage - $now_cool_page_ceil + $i;
  120.             }
  121.             if($page > 0 && $page != $this->nowPage){
  122.                 if($page <= $this->totalPages){
  123.                     $link_page .= '<li><a class="num" href="' . $this->url($page) . '">' . $page . '</a></li>';
  124.                 }else{
  125.                     break;
  126.                 }
  127.             }else{
  128.                 if($page > 0 && $this->totalPages != 1){
  129.                     $link_page .= '<li class="am-active"><a href="#">' . $page . '</a></li>';
  130.                 }
  131.             }
  132.         } 
  133.         //替换分页内容
  134.         $page_str = str_replace(
  135.             array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
  136.             array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
  137.             $this->config['theme']);
  138.             return "<ul class='am-pagination am-pagination-right'>{$page_str}</ul>
  139.                     <div class='am-dropdown  am-dropdown-up' data-am-dropdown>
  140.                         <button class='am-btn am-btn-primary am-dropdown-toggle' data-am-dropdown-toggle>显示条数 <span class='am-icon-caret-up'></span></button>
  141.                         <ul class='am-dropdown-content'>
  142.                             <li><a href='".$this->urlNum(10,1)."'>10</a></li>
  143.                             <li><a href='".$this->urlNum(30,1)."'>30</a></li>
  144.                             <li><a href='".$this->urlNum(50,1)."'>50</a></li>
  145.                         </ul>
  146.                     </div>";
  147.     }
  148. }

效果如下:


5ec5fc995586c.jpg


以上就是thinkphp增加每页显示条数的方法的详细内容,具体美化样式大家自己做一下。



TAG标签:
本文网址:https://www.zztuku.com/index.php/detail-7781.html
站长图库 - thinkphp增加每页显示条数的方法
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐