分享ThinkPHP 3.2 数据分页实例代码
分类:PHP_Python| 发布:kuabaobao| 查看:359 | 发表时间:2014/12/19
TP3.2框架手册,有一个数据分页,不过每次都要写太多的代码,还有中文设置等有些麻烦,做为程序开发者,有必要整理下:
O、先看效果图

一、分页方法
代码如下:
08 | function getpage(& $m , $where , $pagesize =10){ |
10 | $count = $m ->where( $where )-> count (); |
12 | $p = new Think\Page( $count , $pagesize ); |
14 | $p ->setConfig( 'header' , '<li class="rows">共<b>%TOTAL_ROW%</b>条记录 每页<b>%LIST_ROW%</b>条 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>' ); |
15 | $p ->setConfig( 'prev' , '上一页' ); |
16 | $p ->setConfig( 'next' , '下一页' ); |
17 | $p ->setConfig( 'last' , '末页' ); |
18 | $p ->setConfig( 'first' , '首页' ); |
19 | $p ->setConfig( 'theme' , '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' ); |
20 | $p ->parameter=I( 'get.' ); |
21 | $m ->limit( $p ->firstRow, $p ->listRows); |
getpage方法可以放在TP框架的 Application/Common/Common/function.php,这个文档可以专门放置一些通用的方法,在哪里都可以调用(如:Controller文件,View文件等)。
二、调用分页方法
代码如下:
2 | $p =getpage( $m , $where ,10); |
3 | $list = $m ->field(true)->where( $where )->order( 'id desc' )->select(); |
5 | $this ->page= $p ->show(); |
7 | <div class = "pagination" > |
三、最后就是分页的样式了,这个有些乱,因后台框架网上下载的,样式还没来的及整理,这个样式也可以自己实现,简单的。
代码如下:
02 | display: inline-block; |
05 | -webkit-border-radius: 3px; |
06 | -moz-border-radius: 3px; |
08 | -webkit-box-shadow: 0 1px 2px rgba(0,0,0,0.05); |
09 | -moz-box-shadow: 0 1px 2px rgba(0,0,0,0.05); |
10 | box-shadow: 0 1px 2px rgba(0,0,0,0.05); |
15 | .pagination ul li.rows { |
19 | .pagination ul li.rows b{color: #f00} |
20 | .pagination ul li a, .pagination ul li span { |
24 | text-decoration: none; |
25 | background-color: #fff; |
26 | background: url( '../images/bottom_bg.png' ) 0px 0px; |
27 | border: 1px solid #d3dbde; |
32 | .pagination ul li a:hover{ |
36 | .pagination ul li.first-child a, .pagination ul li.first-child span { |
37 | border-left-width: 1px; |
38 | -webkit-border-bottom-left-radius: 3px; |
39 | border-bottom-left-radius: 3px; |
40 | -webkit-border-top-left-radius: 3px; |
41 | border-top-left-radius: 3px; |
42 | -moz-border-radius-bottomleft: 3px; |
43 | -moz-border-radius-topleft: 3px; |
45 | .pagination ul .disabled span, .pagination ul .disabled a, .pagination ul .disabled a:hover { |
48 | background-color: transparent; |
50 | .pagination ul .active a, .pagination ul .active span { |
54 | .pagination ul li a:hover, .pagination ul .active a, .pagination ul .active span { |
55 | background-color: #f0c040; |
57 | .pagination ul li.last-child a, .pagination ul li.last-child span { |
58 | -webkit-border-top-right-radius: 3px; |
59 | border-top-right-radius: 3px; |
60 | -webkit-border-bottom-right-radius: 3px; |
61 | border-bottom-right-radius: 3px; |
62 | -moz-border-radius-topright: 3px; |
63 | -moz-border-radius-bottomright: 3px; |
65 | .pagination ul li.current a{color: #f00 ;font-weight: bold; background: #ddd} |