paging_class.php
11.2 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
362
363
364
365
366
367
368
<?php
/**
* Tiny - A PHP Framework For Web Artisans
* @author Tiny <tinylofty@gmail.com>
* @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
* @version 1.0
*/
/**
* 分页类
*
* @author Tiny
* @class Paging
*/
class Paging{
public $total_nums; //总数据数: 必须传入该值,以数组的形式传入 用mysql_num_rows 获得该值
public $pagesize; //每页显示条数 : 必须传入该值
private $totalpage; //总页数:$total_nums/$pagesize
private $page; //当前的页数
public $pagename = 'p'; //可选:"页"的名字 默认为"p"
private $first_row; //必须写在select语句中的limit后
private $url=''; //当前页的url
public $plus; //当前页与前后的"间距"
private $url1; //取出page后的url 因为在select里要取得this.value的值
private $ajax=false;
/**
* 初始化数据
*
* @access public
* @param array $data
* @return mixed
*/
public function __construct($data=array())
{
$this->total_nums=$data["total_nums"];
$this->pagesize=$data["pagesize"];
$this->pagename=!empty($data["pagename"])?$data["pagename"]:"p";
$this->totalpage=ceil($this->total_nums/$this->pagesize);
$this->plus=!empty($data["plus"]) && $data["plus"]>=0 && $data["plus"]<$this->totalpage/2?intval($data["plus"]):ceil($this->totalpage/2);
$this->page=isset($data['page'])?intval($data['page']):intval(!empty($_GET[$this->pagename])?$_GET[$this->pagename]:"1");
$this->page=$this->page >= $this->totalpage?$this->page=$this->totalpage:$this->page;
$this->page=$this->page<=0?"1":$this->page;
$this->first_row=($this->page-1)*$this->pagesize;
$this->ajax = (isset($data['ajax']) && $data['ajax']==true)?true:false;
$this->ajaxFunction = (isset($data['ajaxFunction']) && $data['ajaxFunction']!='')?$data['ajaxFunction']:'void(0)';
$this->url = isset($data['url'])?$data['url']:'';
}
public function currentPage()
{
return $this->page;
}
public function getFirstRow()
{
return $this->first_row;
}
/**
* 取得url路径
*
* @access private
* @param mixed $page
* @return mixed
*/
private function _get_url($page)
{
if($this->url=='')$url = Url::requestUri();//$_SERVER['REQUEST_URI'];
else $url = $this->url;
$url = urldecode($url);
$arr = array('<'=>'','>'=>'',"'"=>'','"'=>'',' '=>'');
$url = strtr($url,$arr);
$parse = parse_url($url);
$fragment = "";
if(isset($parse['fragment']))
{
$fragment = '#'.$parse['fragment'];
}
if(isset($parse['query']))
{
parse_str($parse['query'],$params);//返回的$params是一个数组
$path = $parse['path'];
$urlFormat = Url::getUrlFormat();
if( $urlFormat != 'get' ){
$path = preg_replace("/\/$this->pagename\/\d+/","",$path);
}
unset($params[$this->pagename]);
$url = $path.'?'.http_build_query($params);
}
else$url .= '?';
if(!empty($params))
{
$url .= '&';
}
$this->url1=$url.$this->pagename."=";
$this->url = $url. $this->pagename . '=' . $page.$fragment;
return $this->url ;
}
/**
* 针对ajax方式的特殊处理
*
* @access private
* @param mixed $page
* @param mixed $text
* @return mixed
*/
private function _get_link($page,$text)
{
$url=$this->_get_url($page);
if($this->ajax){
$onclick = preg_replace('/#page#/',$page,$this->ajaxFunction);
return "<a href='javascript:;' onclick='".$onclick."' page-index='".$page."'>$text</a> ";
}
else return "<a href=$url>$text</a> ";
}
/**
* 生成连接 无效
*
* @access private
* @param mixed $page
* @param mixed $text
* @return mixed
*/
private function _get_link1($page,$text){
$url=$this->_get_url($page);
return "<span href=$url class='disabled'>$text</span> ";
}
/**
* 反回第一面的连接
*
* @access private
* @param string $name
* @return mixed
*/
private function first_page($name="第一页"){
return $this->_get_link(1, $name);
}
/**
* 有省略的第一页
*
* @access private
* @return mixed
*/
private function first_page2(){
$plus=$this->plus;
if($this->page - $plus <= 1){
return "";
}else{
return $this->_get_link(1, 1)."<span class='pageMore'>...</span>\n";
}
}
/**
* 上一页连接
*
* @access private
* @param string $name
* @return mixed
*/
private function up_page($name="上一页"){
if($this->page>1){
return $this->_get_link($this->page - 1, $name);
}else{
return $this->_get_link1(1, $name);
}
}
/**
* 生成下一页
*
* @access private
* @param string $name
* @return mixed
*/
private function down_page($name="下一页"){
if ($this->page<$this->totalpage){
return $this->_get_link($this->page+1, $name);
}else{
return $this->_get_link1($this->page+1, $name);
}
}
/**
* 生成最后一页
*
* @access private
* @param string $name
* @return mixed
*/
private function last_page($name="最后一页"){
return $this->_get_link($this->totalpage, $name);
}
/**
* 生成样式2的"最后一页"
*
* @access private
* @return mixed
*/
private function last_page2(){
$plus=$this->plus;
if($this->page+$plus >= $this->totalpage){
return "";
}else{
return "<span class='pageMore'>...</span>\n".$this->_get_link($this->totalpage, $this->totalpage);
}
}
/**
* 生成中间的数字
*
* @access private
* @return mixed
*/
private function show_nums(){
$plus=$this->plus;
$content=" ";
$begin=$this->page-$plus;
$begin=$begin>=1?$begin:1;//开始的处理方法: 不能从负数开始
$end=($this->page+$plus >= $this->totalpage)?$this->totalpage:($this->page+$plus);//尾部的处理方法:不能超过总页数
if($begin>3){
$content .= $this->_get_link(1, 1).$this->_get_link(2, 2).'...';
}
else if($begin>2){
$content .= $this->_get_link(1, 1).$this->_get_link(2, 2);
}
else if($begin>1){
$content .= $this->_get_link(1, 1);
}
for($i=$begin;$i<=$end;$i++){
if($i==$this->page){
$content .= "<span class='current'>$i</span>\n";
}else{
$content.=$this->_get_link($i, $i);
}
}
if($end<$this->totalpage-1) $content .= '...'.$this->_get_link($this->totalpage, $this->totalpage);
else if($end<$this->totalpage) $content .= $this->_get_link($this->totalpage, $this->totalpage);
return $content;
}
/**
* 第一种样式
*
* @access private
* @return mixed
*/
private function show_1(){
//$return= $this->first_page();
$return="";
$return.= $this->up_page();
$return.=$this->show_nums();
$url1=$this->url1;
$return.= $this->down_page();
if($this->ajax){
$return.= " 共{$this->totalpage} 页 跳到第 <input style='width:24px;text-align:center' value='".$this->page."' onchange='$(this).next().attr(\"page-index\",this.value)'/> 页 <a href='javascript:;' page-index='".$this->page."'>确定</a>";
}else{
$rand = md5(time().rand(10000,99999));
$return.= " 共{$this->totalpage} 页 跳到第 <input id='page_input_".$rand."' style='width:24px;text-align:center' value='".$this->page."' /> 页 <a href='javascript:;' onclick='javascript:window.location.href=\"$url1\"+document.getElementById(\"page_input_".$rand."\").value;' >确定</a>";
}
//$return.= $this->last_page();
return $return;
}
/**
* 第二种样式
*
* @access private
* @return mixed
*/
private function show_2(){
$return= $this->up_page($name="<上一页");
$return.=" ";
//$return.= $this->first_page2($name="1");
$return.=" ";
$return.=$this->show_nums();
//$return.= $this->last_page2();
$return.=" ";
$return.= $this->down_page($name="下一页>");
return $return;
}
/**
* 第三种样式
*
* @access private
* @return mixed
*/
private function show_3(){
$return="总计 $this->total_nums 条记录 $this->page / $this->totalpage 页";
$return.=$this->first_page();
$return.=$this->up_page();
$return.=$this->down_page();
$return.=$this->last_page();
$url1=$this->url1;
$return.="到第 <select id='select' onchange='window.location.href=\"$url1\"+this.value'>";
for($i=1;$i<=$this->totalpage;$i++){
if ($i == $this->page){
$return.= "<option value='$i' selected>$i</option>";
}else{
$return.= "<option value='$i'>$i</option>";
}
}
$return.= "</select> 页 ";
return $return;
}
/**
* 第四种样式
*
* @access private
* @return mixed
*/
private function show_4(){
$return= $this->up_page($name="上一页");
$return.=" ";
$return.=$this->show_nums();
$return.=" ";
$return.= $this->down_page($name="下一页");
return $return;
}
private function show_5(){
$return= $this->up_page($name="上一页");
$return.=" ";
$return.="<span class='current'>".$this->page."</span>\n";
$return.=" ";
$return.= $this->down_page($name="下一页");
return $return;
}
/**
* 封装显示
*
* @access public
* @param int $num
* @return mixed
*/
public function show($num=1){
switch ($num){
case "2":
return $this->show_2();
break;
case "3":
return $this->show_3();
break;
case "4":
return $this->show_4();
break;
case "5":
return $this->show_5();
break;
default :
return $this->show_1();
break;
}
}
public function getTotalPage(){
return $this->totalpage;
}
}