logo头像

小短腿儿努力奔跑的日子

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

废话不多说先上图预览下;即本博客的分页;

白俊遥博客

这个分页类是在thinkphp框架内置的分页类的基础上修改而来;

原分页类的一些设计,在实际运用中感觉不是很方便;

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

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

白俊遥博客

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

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

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

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

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

3:默认就显示首页和末页按钮;

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

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

示例环境:thinkphp3.2.3;

分页类目录:/Thinkphp/Library/Org/Bjy/Page.class.PHP

分页类代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php  

namespace Org\Bjy;

class Page{
public $firstRow; // 起始行数
public $listRows; // 列表每页显示行数
public $parameter; // 分页跳转时要带的参数
public $totalRows; // 总行数
public $totalPages; // 分页总页面数
public $rollPage = 5;// 分页栏每页显示的页数
public $lastSuffix = true; // 最后一页是否显示总页数

private $p = 'p'; //分页参数名
private $url = ''; //当前链接URL
private $nowPage = 1;

// 分页显示定制
private $config = array(
'header' =&gt; '&lt;span class="rows"&gt;共 %TOTAL_ROW% 条记录&lt;/span&gt;',
'first' =&gt; '首页',
'prev' =&gt; '上一页',
'next' =&gt; '下一页',
'last' =&gt; '末页',
'theme' =&gt; '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%',
);

/**
* 架构函数
* @param array $totalRows 总的记录数
* @param array $listRows 每页显示记录数
* @param array $parameter 分页跳转的参数
*/
public function __construct($totalRows, $listRows=20, $parameter = array()) {
C('VAR_PAGE') &amp;&amp; $this-&gt;p = C('VAR_PAGE'); //设置分页参数名称
/* 基础设置 */
$this-&gt;totalRows = $totalRows; //设置总记录数
$this-&gt;listRows = $listRows; //设置每页显示行数
$this-&gt;parameter = empty($parameter) ? $_GET : $parameter;
$this-&gt;nowPage = empty($_GET[$this-&gt;p]) ? 1 : intval($_GET[$this-&gt;p]);
$this-&gt;nowPage = $this-&gt;nowPage&gt;0 ? $this-&gt;nowPage : 1;
$this-&gt;firstRow = $this-&gt;listRows * ($this-&gt;nowPage - 1);
}

/**
* 定制分页链接设置
* @param string $name 设置名称
* @param string $value 设置值
*/
public function setConfig($name,$value) {
if(isset($this-&gt;config[$name])) {
$this-&gt;config[$name] = $value;
}
}

/**
* 生成链接URL
* @param integer $page 页码
* @return string
*/
private function url($page){
return str_replace(urlencode('[PAGE]'), $page, $this-&gt;url);
}

/**
* 组装分页链接
* @return string
*/
public function show() {
if(0 == $this-&gt;totalRows) return '';

/* 生成URL */
$this-&gt;parameter[$this-&gt;p] = '[PAGE]';
$this-&gt;url = U(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME, $this-&gt;parameter);
/* 计算分页信息 */
$this-&gt;totalPages = ceil($this-&gt;totalRows / $this-&gt;listRows); //总页数
if(!empty($this-&gt;totalPages) &amp;&amp; $this-&gt;nowPage &gt; $this-&gt;totalPages) {
$this-&gt;nowPage = $this-&gt;totalPages;
}

/* 计算分页零时变量 */
$now_cool_page = $this-&gt;rollPage/2;
$now_cool_page_ceil = ceil($now_cool_page);

//上一页
$up_row = $this-&gt;nowPage - 1;
$up_page = $up_row &gt; 0 ? '&lt;a class="prev" href="' . $this-&gt;url($up_row) . '"&gt;' . $this-&gt;config['prev'] . '&lt;/a&gt;' : '&lt;a class="prev not-allowed" href="javascript:;"&gt;' . $this-&gt;config['prev'] . '&lt;/a&gt;';

//下一页
$down_row = $this-&gt;nowPage + 1;
$down_page = ($down_row &lt;= $this-&gt;totalPages) ? '&lt;a class="next" href="' . $this-&gt;url($down_row) . '"&gt;' . $this-&gt;config['next'] . '&lt;/a&gt;' : '&lt;a class="next not-allowed" href="javascript:;"&gt;' . $this-&gt;config['next'] . '&lt;/a&gt;';

//第一页
$the_first = '&lt;a class="first" href="' . $this-&gt;url(1) . '"&gt;' . $this-&gt;config['first'] . '&lt;/a&gt;';

//最后一页
$the_end = '&lt;a class="end" href="' . $this-&gt;url($this-&gt;totalPages) . '"&gt;' . $this-&gt;config['last'] . '&lt;/a&gt;';

//数字连接
$link_page = "";
for($i = 1; $i &lt;= $this-&gt;rollPage; $i++){
if(($this-&gt;nowPage - $now_cool_page) &lt;= 0 ){
$page = $i;
}elseif(($this-&gt;nowPage + $now_cool_page - 1) &gt;= $this-&gt;totalPages){
$page = $this-&gt;totalPages - $this-&gt;rollPage + $i;
}else{
$page = $this-&gt;nowPage - $now_cool_page_ceil + $i;
}
if ($page&gt;0) {
if($page != $this-&gt;nowPage){
if($page &lt;= $this-&gt;totalPages){
$link_page .= '&lt;a class="num" href="' . $this-&gt;url($page) . '"&gt;' . $page . '&lt;/a&gt;';
}else{
break;
}
}else{
$link_page .= '&lt;span class="current"&gt;' . $page . '&lt;/span&gt;';
}
}

}

//替换分页内容
$page_str = str_replace(
array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
array($this-&gt;config['header'], $this-&gt;nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this-&gt;totalRows, $this-&gt;totalPages),
$this-&gt;config['theme']);
return '&lt;div class="page"&gt;'.$page_str.'&lt;/div&gt;';
}
}

分页类调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
$count=$this->where($where)->count();  
$page=new \Org\Bjy\Page($count,$limit);
$list=$this->where($where)->order('addtime desc')->limit($page->firstRow.','.$page->listRows)->select();
$show=$page->show();
```


分页类css

```css
.b-page {
background: #fff;
box-shadow: 0px 1px 2px 0px #E2E2E2;
}
.page {
width: 100%;
padding: 30px 15px;
background: #FFF;
text-align: center;
overflow: hidden;
}
.page .first,
.page .prev,
.page .current,
.page .num,
.page .current,
.page .next,
.page .end {
padding: 8px 16px;
margin: 0px 5px;
display: inline-block;
color: #008CBA;
border: 1px solid #F2F2F2;
border-radius: 5px;
}
.page .first:hover,
.page .prev:hover,
.page .current:hover,
.page .num:hover,
.page .current:hover,
.page .next:hover,
.page .end:hover {
text-decoration: none;
background: #F8F5F5;
}
.page .current {
background-color: #008CBA;
color: #FFF;
border-radius: 5px;
border: 1px solid #008CBA;
}
.page .current:hover {
text-decoration: none;
background: #008CBA;
}
.page .not-allowed {
cursor: not-allowed;
}

分页类的使用方法和原thinkphp相同。

支付宝打赏 微信打赏

爷,玩儿得可好啊~