|
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: 麦当苗儿 <[email protected]> <http://www.zjzit.cn> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
namespace app\libraries; |
|
12
|
|
|
use Kotori\Http\Route; |
|
13
|
|
|
|
|
14
|
|
|
class Page |
|
15
|
|
|
{ |
|
16
|
|
|
public $firstRow; // 起始行数 |
|
17
|
|
|
public $listRows; // 列表每页显示行数 |
|
18
|
|
|
public $parameter; // 分页跳转时要带的参数 |
|
19
|
|
|
public $totalRows; // 总行数 |
|
20
|
|
|
public $totalPages; // 分页总页面数 |
|
21
|
|
|
public $rollPage = 11; // 分页栏每页显示的页数 |
|
22
|
|
|
public $lastSuffix = true; // 最后一页是否显示总页数 |
|
23
|
|
|
|
|
24
|
|
|
private $p = 'p'; //分页参数名 |
|
25
|
|
|
private $url = ''; //当前链接URL |
|
26
|
|
|
public $nowPage = 1; |
|
27
|
|
|
|
|
28
|
|
|
// 分页显示定制 |
|
29
|
|
|
private $config = array( |
|
30
|
|
|
'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>', |
|
31
|
|
|
'prev' => '<<', |
|
32
|
|
|
'next' => '>>', |
|
33
|
|
|
'first' => '1...', |
|
34
|
|
|
'last' => '...%TOTAL_PAGE%', |
|
35
|
|
|
'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%', |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* 架构函数 |
|
40
|
|
|
* @param array $totalRows 总的记录数 |
|
41
|
|
|
* @param array $listRows 每页显示记录数 |
|
42
|
|
|
* @param array $parameter 分页跳转的参数 |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($totalRows, $listRows = 20, $parameter = array()) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->p = 'p'; //设置分页参数名称 |
|
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
|
|
|
* 定制分页链接设置 |
|
58
|
|
|
* @param string $name 设置名称 |
|
59
|
|
|
* @param string $value 设置值 |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setConfig($name, $value) |
|
62
|
|
|
{ |
|
63
|
|
|
if (isset($this->config[$name])) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->config[$name] = $value; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* 生成链接URL |
|
71
|
|
|
* @param integer $page 页码 |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
private function url($page) |
|
75
|
|
|
{ |
|
76
|
|
|
return str_replace(urlencode('[PAGE]'), $page, $this->url); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* 组装分页链接 |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function show() |
|
84
|
|
|
{ |
|
85
|
|
|
if (0 == $this->totalRows) |
|
86
|
|
|
{ |
|
87
|
|
|
return ''; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/* 生成URL */ |
|
91
|
|
|
$this->parameter[$this->p] = '[PAGE]'; |
|
92
|
|
|
$this->url = Route::getSoul()->url(URI . '?' . http_build_query($this->parameter)); |
|
93
|
|
|
/* 计算分页信息 */ |
|
94
|
|
|
$this->totalPages = ceil($this->totalRows / $this->listRows); //总页数 |
|
95
|
|
|
if (!empty($this->totalPages) && $this->nowPage > $this->totalPages) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->nowPage = $this->totalPages; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/* 计算分页临时变量 */ |
|
101
|
|
|
$now_cool_page = $this->rollPage / 2; |
|
102
|
|
|
$now_cool_page_ceil = ceil($now_cool_page); |
|
103
|
|
|
$this->lastSuffix && $this->config['last'] = $this->totalPages; |
|
104
|
|
|
|
|
105
|
|
|
//上一页 |
|
106
|
|
|
$up_row = $this->nowPage - 1; |
|
107
|
|
|
$up_page = $up_row > 0 ? '<a class="prev" href="' . $this->url($up_row) . '">' . $this->config['prev'] . '</a>' : ''; |
|
108
|
|
|
|
|
109
|
|
|
//下一页 |
|
110
|
|
|
$down_row = $this->nowPage + 1; |
|
111
|
|
|
$down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="' . $this->url($down_row) . '">' . $this->config['next'] . '</a>' : ''; |
|
112
|
|
|
|
|
113
|
|
|
//第一页 |
|
114
|
|
|
$the_first = ''; |
|
115
|
|
|
if ($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1) |
|
116
|
|
|
{ |
|
117
|
|
|
$the_first = '<a class="first" href="' . $this->url(1) . '">' . $this->config['first'] . '</a>'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
//最后一页 |
|
121
|
|
|
$the_end = ''; |
|
122
|
|
|
if ($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages) |
|
123
|
|
|
{ |
|
124
|
|
|
$the_end = '<a class="end" href="' . $this->url($this->totalPages) . '">' . $this->config['last'] . '</a>'; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
//数字连接 |
|
128
|
|
|
$link_page = ""; |
|
129
|
|
|
for ($i = 1; $i <= $this->rollPage; $i++) |
|
130
|
|
|
{ |
|
131
|
|
|
if (($this->nowPage - $now_cool_page) <= 0) |
|
132
|
|
|
{ |
|
133
|
|
|
$page = $i; |
|
134
|
|
|
} |
|
135
|
|
|
elseif (($this->nowPage + $now_cool_page - 1) >= $this->totalPages) |
|
136
|
|
|
{ |
|
137
|
|
|
$page = $this->totalPages - $this->rollPage + $i; |
|
138
|
|
|
} |
|
139
|
|
|
else |
|
140
|
|
|
{ |
|
141
|
|
|
$page = $this->nowPage - $now_cool_page_ceil + $i; |
|
142
|
|
|
} |
|
143
|
|
|
if ($page > 0 && $page != $this->nowPage) |
|
144
|
|
|
{ |
|
145
|
|
|
|
|
146
|
|
|
if ($page <= $this->totalPages) |
|
147
|
|
|
{ |
|
148
|
|
|
$link_page .= '<a class="num" href="' . $this->url($page) . '">' . $page . '</a>'; |
|
149
|
|
|
} |
|
150
|
|
|
else |
|
151
|
|
|
{ |
|
152
|
|
|
break; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
else |
|
156
|
|
|
{ |
|
157
|
|
|
if ($page > 0 && $this->totalPages != 1) |
|
158
|
|
|
{ |
|
159
|
|
|
$link_page .= '<span class="current">' . $page . '</span>'; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
//替换分页内容 |
|
165
|
|
|
$page_str = str_replace( |
|
166
|
|
|
array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'), |
|
167
|
|
|
array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages), |
|
168
|
|
|
$this->config['theme']); |
|
169
|
|
|
return "<div>{$page_str}</div>"; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|