当前位置: 移动技术网 > IT编程>开发语言>PHP > thinkphp简洁、美观、靠谱的分页类

thinkphp简洁、美观、靠谱的分页类

2018年09月10日  | 移动技术网IT编程  | 我要评论

一万元存一年利息,周一围个人资料,实践论 读后感

我们要实现如下图分页效果

这个分页类是在thinkphp框架内置的分页类的基础上修改而来;
原分页类的一些设计,在实际运用中感觉不是很方便;

1、只有一页内容时不显示分页;

2、原分页类在当前页是第一页和最后一页的时候,不显示第一页和最后一页的按钮;

3、分页数比较少时不显示首页和末页按钮;

4、包裹分页内容的父级div没有class;

5、针对以上问题逐一进行了修改成如下;

6、如果没有数据不显示分页,如果有一页及以上内容即显示分页;

7、默认就显示第一页和最后一页按钮,但是在当前页是第一页和最后一页的时候按钮点击无效果;

8、默认就显示首页和末页按钮;

9、为包裹分页内容的父级div添加名为page的class;

  显示总共查出的内容条数;

       示例环境:thinkphp3.2.3;

分页类目录:/thinkphp/library/org/bjy/page.class.php
分页类代码如下:

  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 /*
 12  * php分页类
 13  * 修改者:白俊遥
 14  * 日  期:2015.5.10
 15  * 邮  箱:baijunyao@baijunyao.com
 16  * 博  客:http://baijunyao.com
 17  */
 18 namespace org\bjy;
 19 class page{
 20     public $firstrow; // 起始行数
 21     public $listrows; // 列表每页显示行数
 22     public $parameter; // 分页跳转时要带的参数
 23     public $totalrows; // 总行数
 24     public $totalpages; // 分页总页面数
 25     public $rollpage   = 5;// 分页栏每页显示的页数
 26     public $lastsuffix = true; // 最后一页是否显示总页数
 27     private $p       = 'p'; //分页参数名
 28     private $url     = ''; //当前链接url
 29     private $nowpage = 1;
 30     // 分页显示定制
 31     private $config  = array(
 32         'header' => '<span class="rows">共 %total_row% 条记录</span>',
 33         'first'   => '首页',
 34         'prev'   => '上一页',
 35         'next'   => '下一页',
 36         'last'   => '末页',
 37         'theme'  => '%first% %up_page% %link_page% %down_page% %end% %header%',
 38     );
 39     /**
 40      * 架构函数
 41      * @param array $totalrows  总的记录数
 42      * @param array $listrows  每页显示记录数
 43      * @param array $parameter  分页跳转的参数
 44      */
 45     public function __construct($totalrows, $listrows=20, $parameter = array()) {
 46         c('var_page') && $this->p = c('var_page'); //设置分页参数名称
 47         /* 基础设置 */
 48         $this->totalrows  = $totalrows; //设置总记录数
 49         $this->listrows   = $listrows;  //设置每页显示行数
 50         $this->parameter  = empty($parameter) ? $_get : $parameter;
 51         $this->nowpage    = empty($_get[$this->p]) ? 1 : intval($_get[$this->p]);
 52         $this->nowpage    = $this->nowpage>0 ? $this->nowpage : 1;
 53         $this->firstrow   = $this->listrows * ($this->nowpage - 1);
 54     }
 55     /**
 56      * 定制分页链接设置
 57      * @param string $name  设置名称
 58      * @param string $value 设置值
 59      */
 60     public function setconfig($name,$value) {
 61         if(isset($this->config[$name])) {
 62             $this->config[$name] = $value;
 63         }
 64     }
 65     /**
 66      * 生成链接url
 67      * @param  integer $page 页码
 68      * @return string
 69      */
 70     private function url($page){
 71         return str_replace(urlencode('[page]'), $page, $this->url);
 72     }
 73     /**
 74      * 组装分页链接
 75      * @return string
 76      */
 77     public function show() {
 78         if(0 == $this->totalrows) return '';
 79         /* 生成url */
 80         $this->parameter[$this->p] = '[page]';
 81         $this->url = u(module_name.'/'.controller_name.'/'.action_name, $this->parameter);
 82         /* 计算分页信息 */
 83         $this->totalpages = ceil($this->totalrows / $this->listrows); //总页数
 84         if(!empty($this->totalpages) && $this->nowpage > $this->totalpages) {
 85             $this->nowpage = $this->totalpages;
 86         }
 87         /* 计算分页零时变量 */
 88         $now_cool_page      = $this->rollpage/2;
 89         $now_cool_page_ceil = ceil($now_cool_page);
 90         //上一页
 91         $up_row  = $this->nowpage - 1;
 92         $up_page = $up_row > 0 ? '<a class="prev" href="' . $this->url($up_row) . '">' . $this->config['prev'] . '</a>' : '<a class="prev not-allowed" href="javascript:;">' . $this->config['prev'] . '</a>';
 93         //下一页
 94         $down_row  = $this->nowpage + 1;
 95         $down_page = ($down_row <= $this->totalpages) ? '<a class="next" href="' . $this->url($down_row) . '">' . $this->config['next'] . '</a>' : '<a class="next not-allowed" href="javascript:;">' . $this->config['next'] . '</a>';
 96         //第一页
 97         $the_first = '<a class="first" href="' . $this->url(1) . '">' . $this->config['first'] . '</a>';
 98         //最后一页
 99         $the_end = '<a class="end" href="' . $this->url($this->totalpages) . '">' . $this->config['last'] . '</a>';
100         //数字连接
101         $link_page = "";
102         for($i = 1; $i <= $this->rollpage; $i++){
103             if(($this->nowpage - $now_cool_page) <= 0 ){
104                 $page = $i;
105             }elseif(($this->nowpage + $now_cool_page - 1) >= $this->totalpages){
106                 $page = $this->totalpages - $this->rollpage + $i;
107             }else{
108                 $page = $this->nowpage - $now_cool_page_ceil + $i;
109             }
110             if ($page>0) {
111                 if($page != $this->nowpage){
112                     if($page <= $this->totalpages){
113                         $link_page .= '<a class="num" href="' . $this->url($page) . '">' . $page . '</a>';
114                     }else{
115                         break;
116                     }
117                 }else{
118                     $link_page .= '<span class="current">' . $page . '</span>';
119                 }
120             }
121         }
122         //替换分页内容
123         $page_str = str_replace(
124             array('%header%', '%now_page%', '%up_page%', '%down_page%', '%first%', '%link_page%', '%end%', '%total_row%', '%total_page%'),
125             array($this->config['header'], $this->nowpage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalrows, $this->totalpages),
126             $this->config['theme']);
127         return '<div class="page">'.$page_str.'</div>';
128     }
129 }

分页类调用:

1 $count=$this->where($where)->count();
2 $page=new \org\bjy\page($count,$limit);
3 $list=$this->where($where)->order('addtime desc')->limit($page->firstrow.','.$page->listrows)->select();
4 $show=$page->show();

分页类css

 1 .b-page {
 2   background: #fff;
 3   box-shadow: 0px 1px 2px 0px #e2e2e2;
 4 }
 5 .page {
 6   width: 100%;
 7   padding: 30px 15px;
 8   background: #fff;
 9   text-align: center;
10   overflow: hidden;
11 }
12 .page .first,
13 .page .prev,
14 .page .current,
15 .page .num,
16 .page .current,
17 .page .next,
18 .page .end {
19   padding: 8px 16px;
20   margin: 0px 5px;
21   display: inline-block;
22   color: #008cba;
23   border: 1px solid #f2f2f2;
24   border-radius: 5px;
25 }
26 .page .first:hover,
27 .page .prev:hover,
28 .page .current:hover,
29 .page .num:hover,
30 .page .current:hover,
31 .page .next:hover,
32 .page .end:hover {
33   text-decoration: none;
34   background: #f8f5f5;
35 }
36 .page .current {
37   background-color: #008cba;
38   color: #fff;
39   border-radius: 5px;
40   border: 1px solid #008cba;
41 }
42 .page .current:hover {
43   text-decoration: none;
44   background: #008cba;
45 }
46 .page .not-allowed {
47   cursor: not-allowed;
48 }

分页类的使用方法和原thinkphp相同;具体参考:

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网