Page.class.php
20.7 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/*
* 模板分页类,源于Easp的数据库分页方法,算是Easp分页的的PHP独立版
* 支持动态和静态分页方式
* Easp官网http://easp.lengshi.com/
* 作者:钟晶晶
* 日期:2010-11-3
* 邮箱:zhongjingjing@gmail.com
* 博客:http://blog.zaimer.com
* Page([总记录数=1],[分页大小=20],[当前页=1],[显示页数=6],[分页参数='page'],[分页链接=当前页面],[是否静态=FALSE])
* 动态:
* 简单用法:
* $page = new Page(50);
* $page->SetPager('zjj','<div class="newpager">共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
* echo $page->show('zjj');
* 全参数用法:
* $page = new Page(50,20,1,6,'page','prrr.php',false);
* $page->SetPager('zjj','<div class="newpager">共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
* echo $page->show('zjj');
* 静态:
* $page = new Page(50,20,1,6,'page','prrr{page}.html',true);
* $page->SetPager('zjj','<div class="newpager">共有{recordcount} 个商品 当前第 {pageindex} 页 / 共 {pagecount} 页 分页: {first}{prev} {list} {next}{last} 转到 {jump} 页</div>',array("listlong"=>"6","first"=>"首页","last"=>"尾页","prev"=>"上一页","next"=>"下一页","list"=>"第*页","jump"=>"select"));
* echo $page->show('zjj');
其他说明:
$page = new Page($Total_Size , $Page_Size $Current_Page , $List_Page, $PageParam , $PageLink ,$Static);
SetPager()方法设置分页导航列表样式语法
$page->SetPager($name, $html, $config);
参数说明:
$name:
String (字符串) 分页导航样式配置名称,默认样式的名称为”default”
$html:
String (字符串)
分页导航样式HTML模板,可以用以下代码嵌入HTML代码中代表相应的项目(均为可选):
“{recordcount}” - 总记录数
“{pagecount}” - 总页数
“{pageindex}” - 当前页码
“{pagesize}” - 每页记录数
“{list}” - 分页链接列表
“{liststart}” - 分页链接列表页首导航
“{listend}” - 分页链接列表页尾导航
“{first}” - 第一页的链接
“{last}” - 最后页的链接
“{prev}” - 上一页的链接
“{next}” - 下一页的链接
“{jump}” - 页面跳转文本框或下拉菜单
$config:
“” (空字符串) 或 Array (数组)
分页导航样式配置选项。如果留空将采用默认配置,否则使用数组配置,可配置项目包括:
“listlong” - 分页链接列表页码数量,默认为9
“listsidelong” - 分页链接列表首尾导航页码数量,默认为2,html 参数中有”{liststart}”或”{listend}”时才有效
“list” - 分页链接的HTML代码,用*代表页码,默认为仅显示页码
“currentclass” - 当前页码的CSS样式名称,默认为”current”
“link” - 自定义页码链接,用*代表页码,用于静态页面分页或Ajax分页
“first” - 第一页链接的HTML代码,默认为 ”«”,即显示为 «
“prev” - 上一页链接的HTML代码,默认为”‹”,即显示为 ‹
“next” - 下一页链接的HTML代码,默认为”›”,即显示为 ›
“last” - 最后页链接的HTML代码,默认为”»”,即显示为 »
“more” - 被省略的页码链接显示为,默认为”…”
“disabledclass” - 当处于首尾页时不可用链接的CSS样式名称,默认为”disabled”
“jump” - 页面跳转方式,默认为”input”文本框,可设置为”select”下拉菜单
“jumpplus” - 页面跳转文本框或下拉菜单的附加内部代码
“jumpaction” - 跳转时要执行的javascript代码,用*代表页码,可用于Ajax分页
“jumplong” - 当跳转方式为下拉菜单时最多同时显示的页码数量,0为全部显示,默认为50
*/
class Page {
private $Page_size; //每页显示的条目数
private $Total_Size; //总条目数
private $Current_page; //当前被选中的页
private $List_Page; //每次显示的页数 默认列表每页显示行数
private $Total_Pages = 20; //总页数
private $Page_tpl = array(); // 分页模板
private $PageParam;
private $PageLink;
private $Static;
private $pList;
private $pListEnd;
private $pListStart;
private $pFirst;
private $pPrev;
private $pLast;
private $pNext;
// 起始行数
public $firstRow;
public $listRows;
private $linkwraper="";
private $linkwraper_pre="";
private $linkwraper_after="";
private $searching=false;
//Page([总记录数=1], [分页大小=20], [当前页=1], [显示页数=6], [分页参数='page'], [分页链接=当前页面],[是否静态=FALSE])
function __construct($Total_Size = 1, $Page_Size = 20, $Current_Page = 1, $List_Page = 6, $PageParam = 'p', $PageLink = '', $Static = FALSE) {
$this->Page_size = intval($Page_Size);
$this->Total_Size = intval($Total_Size);
if (!$Current_Page) {
$this->Current_page = 1;
} else {
$this->Current_page = (int)$Current_Page<1?1:(int)$Current_Page;
}
//总分页数
$this->Total_Pages = ceil($Total_Size / $Page_Size);
//一次显示多少个链接 ,该参数会被SetPager里传入的配置覆盖。
$this->List_Page = (int)$List_Page;
//接收分页参数的标识符
$this->PageParam = $PageParam;
//当前页面地址,当需要生成静态地址,此参数需要给,分页号用{page}
$this->PageLink = (empty($PageLink) ? $_SERVER ["PHP_SELF"] : $PageLink);
//是否开启静态
$this->Static = $Static;
$this->Page_tpl ['default'] = array('Tpl' => '<div class="pager">{first}{prev}{liststart}{list}{listend}{next}{last} 跳转到{jump}页</div>', 'Config' => array());
$this->GetCurrentPage();
$this->listRows = $Page_Size;
$this->firstRow = ($this->Current_page - 1) * $this->listRows;
if($this->firstRow < 0){
$this->firstRow = 0;
}
}
public function __set($Param, $value) {
$this->$Param = $value;
}
public function __get($Param) {
return $this->$Param;
}
public function getTotalPages(){
return $this->Total_Pages;
}
public function setLinkWraper($wraper){
if(empty($wraper)){
}else{
$this->linkwraper=$wraper;
$this->linkwraper_after="</$wraper>";
$this->linkwraper_pre="<$wraper>";
}
}
private function UrlParameters($url = array()) {
unset($url[C('VAR_MODULE')]);
unset($url[C('VAR_CONTROLLER')]);
unset($url[C('VAR_ACTION')]);
foreach ($url as $key => $val) {
if ($key != $this->PageParam && $key != "_URL_")
$arg [$key] = $val;
}
$arg[$this->PageParam] = '*';
if ($this->Static) {
//当启用静态地址,$this->PageLink传入的是array,并且包含两个 index,list
/*
* array(
* "index"=>"http://www.a.com/192.html",//这种是表示当前是首页,无需加分页1
* "list"=>"http://www.a.com/192-{page}.html",//这种表示分页非首页时启用
* )
*/
if (is_array($this->PageLink)) {
return str_replace('{page}', '*', $this->PageLink['list']);
} else {
return str_replace('{page}', '*', $this->PageLink);
}
} else {
if($this->searching){
$url=leuu(MODULE_NAME . "/" . CONTROLLER_NAME . "/" . ACTION_NAME)."?".http_build_query ($arg);
}else{
$url=leuu(MODULE_NAME . "/" . CONTROLLER_NAME . "/" . ACTION_NAME,$arg);
}
return str_replace("%2A", "*", $url);
}
}
public function SetPager($Tpl_Name = 'default', $Tpl = '', $Config = array()) {
if (empty($Tpl))
$Tpl = $this->Page_tpl ['default'] ['Tpl'];
if (empty($Config))
$Config = $this->Page_tpl ['default'] ['Config'];
$this->Page_tpl [$Tpl_Name] = array('Tpl' => $Tpl, 'Config' => $Config);
}
public function show($Tpl_Name = 'default') {
//当分页数只有1的时候,不显示
if ($this->Total_Pages <= 1) {
return;
}
return $this->Pager($this->Page_tpl [$Tpl_Name]);
}
public function GetCurrentPage() {
$p=isset($_GET [$this->PageParam])?intval($_GET [$this->PageParam]):1;
session('thispage',$_GET [$this->PageParam]);
$p=$p < 1 ? 1 : $p;
$total_pages=intval($this->Total_Pages);
$this->Current_page = ($p <=$total_pages? $p : $total_pages);
}
public function Pager($Page_tpl = '') {
if (empty($Page_tpl))
$Page_tpl = $this->Page_tpl ['default'];
$_GET=array_merge($_GET,$_POST);
$cfg = array('recordcount' => intval($this->Total_Size), 'pageindex' => intval($this->Current_page), 'pagecount' => intval($this->Total_Pages), 'pagesize' => intval($this->Page_size), 'listlong' => intval($this->List_Page), 'listsidelong' => 2, 'list' => '*', 'currentclass' => 'current', 'link' => $this->UrlParameters($_GET), 'first' => '«', 'prev' => '‹', 'next' => '›', 'last' => '»', 'more' => $this->linkwraper_pre.'<span>...</span>'.$this->linkwraper_after, 'disabledclass' => 'disabled', 'jump' => 'input', 'jumpplus' => '', 'jumpaction' => '', 'jumplong' => 50);
if (!empty($Page_tpl ['Config'])) {
foreach ($Page_tpl ['Config'] as $key => $val) {
if (array_key_exists($key, $cfg))
$cfg [$key] = $val;
}
}
//判断listlong是否为偶数
if( (int)$cfg ['listlong']%2 != 0 ){
$cfg ['listlong'] = $cfg ['listlong']+1;
}
$tmpStr = $Page_tpl ['Tpl'];
$pStart = $cfg ['pageindex'] - (($cfg ['listlong'] / 2) + ($cfg ['listlong'] % 2)) + 1;
$pEnd = $cfg ['pageindex'] + $cfg ['listlong'] / 2;
if ($pStart < 1) {
$pStart = 1;
$pEnd = $cfg ['listlong'];
}
if ($pEnd > $cfg ['pagecount']) {
$pStart = $cfg ['pagecount'] - $cfg ['listlong'] + 1;
$pEnd = $cfg ['pagecount'];
}
if ($pStart < 1)
$pStart = 1;
for ($i = $pStart; $i <= $pEnd; $i++) {
if ($i == $cfg ['pageindex']) {
$wraper= empty($this->linkwraper)?'':'<'.$this->linkwraper.' class="active">';
$this->pList .=$wraper.'<a class="item cur">' . str_replace('*', $i, $cfg ['list']) . '</a> '.$this->linkwraper_after;
} else {
//此处是为了照顾静态地址生成时,第一页不显示当前分页1,启用该方法,静态地址需要$this->PageLink传入的是array,并且包含两个 index,list。index是首页
//事例用法 new Page(50,5,2,6,"page",array("index"=>"http://www.a.com/192.html","list"=>"http://www.a.com/192-{page}.html",),true);
if ($this->Static && $i == 1) {
$this->pList .= $this->linkwraper_pre.'<a class="item" href="' . $this->PageLink['index'] . '"> ' . str_replace('*', $i, $cfg ['list']) . '</a> '.$this->linkwraper_after;
} else {
$this->pList .= $this->linkwraper_pre.'<a class="item" href="' . str_replace('*', $i, $cfg ['link']) . '"> ' . str_replace('*', $i, $cfg ['list']) . '</a> '.$this->linkwraper_after;
}
}
}
if ($cfg ['listsidelong'] > 0) {
if ($cfg ['listsidelong'] < $pStart) {
for ($i = 1; $i <= $cfg ['listsidelong']; $i++) {
if ($this->Static && $i == 1) {
$this->pListStart .= $this->linkwraper_pre.'<a class="item" href="' . $this->PageLink['index'] . '">' . str_replace('*', $i, $cfg ['list']) . '</a> '.$this->linkwraper_after;
} else {
$this->pListStart .= $this->linkwraper_pre.'<a class="item" href="' . str_replace('*', $i, $cfg ['link']) . '">' . str_replace('*', $i, $cfg ['list']) . '</a> '.$this->linkwraper_after;
}
}
$this->pListStart .= ($cfg ['listsidelong'] + 1) == $pStart ? '' : $cfg ['more'] . ' ';
} else {
if ($cfg ['listsidelong'] >= $pStart && $pStart > 1) {
for ($i = 1; $i <= ($pStart - 1); $i++) {
if ($this->Static && $i == 1) {
$this->pListStart .= $this->linkwraper_pre.'<a class="item" href="' . $this->PageLink['index'] . '"> ' . str_replace('*', $i, $cfg ['list']) . '</a> '.$this->linkwraper_after;
} else {
$this->pListStart .= $this->linkwraper_pre.'<a class="item" href="' . str_replace('*', $i, $cfg ['link']) . '"> ' . str_replace('*', $i, $cfg ['list']) . '</a> '.$this->linkwraper_after;
}
}
}
}
if (($cfg ['pagecount'] - $cfg ['listsidelong']) > $pEnd) {
$this->pListEnd = ' ' . $cfg ['more'] . $this->pListEnd;
for ($i = (($cfg ['pagecount'] - $cfg ['listsidelong']) + 1); $i <= $cfg ['pagecount']; $i++) {
if ($this->Static && $i == 1) {
$this->pListEnd .= $this->linkwraper_pre.'<a class="item" href="' . $this->PageLink['index'] . '">' . str_replace('*', $i, $cfg ['list']) . '</a> '.$this->linkwraper_after;
} else {
$this->pListEnd .= $this->linkwraper_pre.'<a class="item" href="' . str_replace('*', $i, $cfg ['link']) . '"> ' . str_replace('*', $i, $cfg ['list']) . ' </a> '.$this->linkwraper_after;
}
}
} else {
if (($cfg ['pagecount'] - $cfg ['listsidelong']) <= $pEnd && $pEnd < $cfg ['pagecount']) {
for ($i = ($pEnd + 1); $i <= $cfg ['pagecount']; $i++) {
if ($this->Static && $i == 1) {
$this->pListEnd .= '<a class="item" href="' . $this->PageLink['index'] . '">' . str_replace('*', $i, $cfg ['list']) . '</a> '.$this->linkwraper_after;
} else {
$this->pListEnd .= $this->linkwraper_pre.'<a class="item" href="' . str_replace('*', $i, $cfg ['link']) . '"> ' . str_replace('*', $i, $cfg ['list']) . ' </a> '.$this->linkwraper_after;
}
}
}
}
}
//上一页 首页
if ($cfg ['pageindex'] > 1) {
if ($this->Static) {
$wraper= empty($this->linkwraper)?$this->linkwraper_pre:'<'.$this->linkwraper.' class="p-index">';
$this->pFirst = $wraper.'<a class="item" href="' . $this->PageLink['index'] . '">' . $cfg ['first'] . '</a> '.$this->linkwraper_after; //首页
} else {
$wraper= empty($this->linkwraper)?$this->linkwraper_pre:'<'.$this->linkwraper.' class="p-index">';
$this->pFirst = $wraper.'<a class="item" href="' . str_replace('*', 1, $cfg ['link']) . '">' . $cfg ['first'] . '</a> '.$this->linkwraper_after; //首页
}
if ($this->Static && ($cfg ['pageindex'] - 1) == 1) {
$wraper= empty($this->linkwraper)?$this->linkwraper_pre:'<'.$this->linkwraper.' class="p-prev">';
$this->pPrev = $wraper.'<a class="prev" href="' . $this->PageLink['index'] . '">' . $cfg ['prev'] . '</a> '.$this->linkwraper_after; //上一页
} else {
$wraper= empty($this->linkwraper)?$this->linkwraper_pre:'<'.$this->linkwraper.' class="p-prev">';
$this->pPrev = $wraper.'<a class="prev" href="' . str_replace('*', $cfg ['pageindex'] - 1, $cfg ['link']) . '">' . $cfg ['prev'] . '</a> '.$this->linkwraper_after;
}
}
//下一页,尾页
if ($cfg ['pageindex'] < $cfg ['pagecount']) {
$wraper_next= empty($this->linkwraper)?$this->linkwraper_pre:'<'.$this->linkwraper.' class="p-next">';
$wraper_end= empty($this->linkwraper)?$this->linkwraper_pre:'<'.$this->linkwraper.' class="p-end">';
$this->pLast = $wraper_next.'<a class="next" href="' . str_replace('*', $cfg ['pagecount'], $cfg ['link']) . '">' . $cfg ['last'] . '</a> '.$this->linkwraper_after;
$this->pNext = $wraper_end.'<a class="next" href="' . str_replace('*', $cfg ['pageindex'] + 1, $cfg ['link']) . '">' . $cfg ['next'] . '</a> '.$this->linkwraper_after;
}
//快捷跳转方式
switch (strtolower($cfg ['jump'])) {
case 'input' :
$pJumpValue = 'this.value';
$pJump = '<input type="text" size="3" title="请输入要跳转到的页数并回车"' . (($cfg ['jumpplus'] == '') ? '' : ' ' . $cfg ['jumpplus']);
$pJump .= ' onkeydown="javascript:if(event.charCode==13||event.keyCode==13){if(!isNaN(' . $pJumpValue . ')){';
$pJump .= ($cfg ['jumpaction'] == '' ? ((strtolower(substr($cfg ['link'], 0, 11)) == 'javascript:') ? str_replace('*', $pJumpValue, substr($cfg ['link'], 12)) : " document.location.href='" . str_replace('*', '\'+' . $pJumpValue . '+\'', $cfg ['link']) . '\';') : str_replace("*", $pJumpValue, $cfg ['jumpaction']));
$pJump .= '}return false;}" />';
break;
case 'select' :
$pJumpValue = "this.options[this.selectedIndex].value";
$pJump = '<select ' . ($cfg ['jumpplus'] == '' ? ' ' . $cfg ['jumpplus'] . ' onchange="javascript:' : $cfg ['jumpplus']);
$pJump .= ($cfg ['jumpaction'] == '' ? ((strtolower(substr($cfg ['link'], 0, 11)) == 'javascript:') ? str_replace('*', $pJumpValue, substr($cfg ['link'], 12)) : " document.location.href='" . str_replace('*', '\'+' . $pJumpValue . '+\'', $cfg ['link']) . '\';') : str_replace("*", $pJumpValue, $cfg ['jumpaction']));
$pJump .= '" title="请选择要跳转到的页数"> ';
if ($cfg ['jumplong'] == 0) {
for ($i = 0; $i <= $cfg ['pagecount']; $i++) {
$pJump .= '<option value="' . $i . '"' . (($i == $cfg ['pageindex']) ? ' selected="selected"' : '') . ' >' . $i . '</option> ';
}
} else {
$pJumpLong = intval($cfg ['jumplong'] / 2);
$pJumpStart = ((($cfg ['pageindex'] - $pJumpLong) < 1) ? 1 : ($cfg ['pageindex'] - $pJumpLong));
$pJumpStart = ((($cfg ['pagecount'] - $cfg ['pageindex']) < $pJumpLong) ? ($pJumpStart - ($pJumpLong - ($cfg ['pagecount'] - $cfg ['pageindex'])) + 1) : $pJumpStart);
$pJumpStart = (($pJumpStart < 1) ? 1 : $pJumpStart);
$j = 1;
for ($i = $pJumpStart; $i <= $cfg ['pageindex']; $i++, $j++) {
$pJump .= '<option value="' . $i . '"' . (($i == $cfg ['pageindex']) ? ' selected="selected"' : '') . '>' . $i . '</option> ';
}
$pJumpLong = $cfg ['pagecount'] - $cfg ['pageindex'] < $pJumpLong ? $pJumpLong : $pJumpLong + ($pJumpLong - $j) + 1;
$pJumpEnd = $cfg ['pageindex'] + $pJumpLong > $cfg ['pagecount'] ? $cfg ['pagecount'] : $cfg ['pageindex'] + $pJumpLong;
for ($i = $cfg ['pageindex'] + 1; $i <= $pJumpEnd; $i++) {
$pJump .= '<option value="' . $i . '">' . $i . '</option> ';
}
}
$pJump .= '</select>';
break;
}
$patterns = array('/{recordcount}/', '/{pagecount}/', '/{pageindex}/', '/{pagesize}/', '/{list}/', '/{liststart}/', '/{listend}/', '/{first}/', '/{prev}/', '/{next}/', '/{last}/', '/{jump}/');
$replace = array($cfg ['recordcount'], $cfg ['pagecount'], $cfg ['pageindex'], $cfg ['pagesize'], $this->pList, $this->pListStart, $this->pListEnd, $this->pFirst, $this->pPrev, $this->pNext, $this->pLast, $pJump);
$tmpStr = chr(13) . chr(10) . preg_replace($patterns, $replace, $tmpStr) . chr(13) . chr(10);
unset($cfg);
return $tmpStr;
}
}