Pagination.php
6.68 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
<?php
class LtPagination
{
public $configHandle;
public $conf;
public function __construct()
{
if (! $this->configHandle instanceof LtConfig)
{
if (class_exists("LtObjectUtil", false))
{
$this->configHandle = LtObjectUtil::singleton("LtConfig");
}
else
{
$this->configHandle = new LtConfig;
}
}
}
public function init()
{
$this->conf = $this->configHandle->get("pagination.pager");
if (empty($this->conf))
{
$this->conf['per_page'] = 25; //每个页面中希望展示的项目数量
$this->conf['num_links_show'] = 9; //数字链接显示数量
$this->conf['num_point_start_end'] = 2; //“点”前边和后边的链接数量
$this->conf['show_first'] = true;
$this->conf['show_prev'] = true;
$this->conf['show_next'] = true;
$this->conf['show_last'] = true;
$this->conf['show_goto'] = false;
$this->conf['show_info'] = false;
$this->conf['show_point'] = true;
$this->conf['show_empty_button'] = false;
$this->conf['first_text'] = 'First';
$this->conf['prev_text'] = 'Prev';
$this->conf['next_text'] = 'Next';
$this->conf['last_text'] = 'Last';
$this->conf['point_text'] = '...';
$this->conf['full_tag_open'] = '<div class="pages">';
$this->conf['full_tag_close'] = '</div>';
$this->conf['num_tag_open'] = '';
$this->conf['num_tag_close'] = '';
$this->conf['link_tag_open'] = '<a href=":url">';
$this->conf['link_tag_close'] = '</a>';
$this->conf['link_tag_cur_open'] = '<strong>';
$this->conf['link_tag_cur_close'] = '</strong>';
$this->conf['button_tag_open'] = '<a href=":url" style="font-weight:bold">';
$this->conf['button_tag_close'] = '</a>';
$this->conf['button_tag_empty_open'] = '<span>';
$this->conf['button_tag_empty_close'] = '</span>';
$this->conf['point_tag_open'] = '<span>';
$this->conf['point_tag_close'] = '</span>';
}
}
/**
*
* @param $page int 当前页
* @param $count int 这个数值是你查询数据库得到的数据总量
* @param $url string 字串中使用 :page 表示页参数 不在意位置 如/a/:page/c/e
*/
public function pager($page, $count, $url)
{
$per_page = empty($this->conf['per_page']) ? 25 : $this->conf['per_page'];
$pagecount = ceil($count / $per_page);
$pager = $this->renderPager($page, $pagecount, $url);
if ($this->conf['show_goto'])
{
$pager .= $this->renderButton('goto', $page, $pagecount, $url);
}
if ($this->conf['show_info'])
{
$pager .= $this->renderButton('info', $page, $pagecount, $url);
}
return $this->conf['full_tag_open'] . $pager . $this->conf['full_tag_close'];
}
/**
*
* @param $pagenumber int 当前页
* @param $pagecount int 总页数
* @return string
*/
public function renderPager($pagenumber, $pagecount, $baseurl = '?page=:page')
{
$baseurl = urldecode($baseurl);
$pager = $this->conf['num_tag_open'];
$pager .= $this->renderButton('first', $pagenumber, $pagecount, $baseurl);
$pager .= $this->renderButton('prev', $pagenumber, $pagecount, $baseurl);
$startPoint = 1;
$endPoint = $this->conf['num_links_show'];
$num_links = ceil($this->conf['num_links_show'] / 2) - 1;
if ($pagenumber > $num_links)
{
$startPoint = $pagenumber - $num_links;
$endPoint = $pagenumber + $num_links;
}
if ($endPoint > $pagecount)
{
$startPoint = $pagecount + 1 - $this->conf['num_links_show'];
$endPoint = $pagecount;
}
if ($startPoint < 1)
{
$startPoint = 1;
}
$currentButton = '';
if ($this->conf['show_point'])
{
for($page = 1; $page < $startPoint; $page++)
{
$url = str_replace(':page', $page, $baseurl);
if ($page > $this->conf['num_point_start_end'])
{
$currentButton .= $this->conf['point_tag_open'] . $this->conf['point_text'] . $this->conf['point_tag_close'];
break;
}
$currentButton .= str_replace(':url', $url, $this->conf['link_tag_open']) . $page . $this->conf['link_tag_close'];
}
}
for ($page = $startPoint; $page <= $endPoint; $page++)
{
$url = str_replace(':page', $page, $baseurl);
if ($page == $pagenumber)
{
$currentButton .= $this->conf['link_tag_cur_open'] . $page . $this->conf['link_tag_cur_close'];
}
else
{
$currentButton .= str_replace(':url', $url, $this->conf['link_tag_open']) . $page . $this->conf['link_tag_close'];
}
}
if ($this->conf['show_point'])
{
$page = $pagecount - $this->conf['num_point_start_end'];
if ($page > $endPoint)
{
$currentButton .= $this->conf['point_tag_open'] . $this->conf['point_text'] . $this->conf['point_tag_close'];
}
for($page += 1; $page >= $endPoint && $page <= $pagecount; $page++)
{
if ($page == $endPoint) continue;
$url = str_replace(':page', $page, $baseurl);
$currentButton .= str_replace(':url', $url, $this->conf['link_tag_open']) . $page . $this->conf['link_tag_close'];
}
}
$pager .= $currentButton;
$pager .= $this->renderButton('next', $pagenumber, $pagecount, $baseurl);
$pager .= $this->renderButton('last', $pagenumber, $pagecount, $baseurl);
$pager .= $this->conf['num_tag_close'];
return $pager;
}
/**
*
* @param $buttonLabel string 显示文字
* @param $pagenumber int 当前页
* @param $pagecount int 总页数
* @return string
*/
public function renderButton($buttonLabel, $pagenumber, $pagecount, $baseurl = '?page=:page')
{
$baseurl = urldecode($baseurl);
$destPage = 1;
if ('goto' == $buttonLabel)
{
$button = "goto <input type=\"text\" size=\"3\" onkeydown=\"javascript: if(event.keyCode==13){ location='{$baseurl}'.replace(':page',this.value);return false;}\" />";
return $button;
}
if ('info' == $buttonLabel)
{
$button = " $pagenumber/$pagecount ";
return $button;
}
switch ($buttonLabel)
{
case "first":
$destPage = 1;
$bottenText = $this->conf['first_text'];
break;
case "prev":
$destPage = $pagenumber - 1;
$bottenText = $this->conf['prev_text'];
break;
case "next":
$destPage = $pagenumber + 1;
$bottenText = $this->conf['next_text'];
break;
case "last":
$destPage = $pagecount;
$bottenText = $this->conf['last_text'];
break;
}
$url = str_replace(':page', $destPage, $baseurl);
$button = str_replace(':url', $url, $this->conf['button_tag_open']) . $bottenText . $this->conf['button_tag_close'];
if ($buttonLabel == "first" || $buttonLabel == "prev")
{
if ($pagenumber <= 1)
{
$button = $this->conf['show_empty_button'] ? $this->conf['button_tag_empty_open'] . $bottenText . $this->conf['button_tag_empty_close'] : '';
}
}
else
{
if ($pagenumber >= $pagecount)
{
$button = $this->conf['show_empty_button'] ? $this->conf['button_tag_empty_open'] . $bottenText . $this->conf['button_tag_empty_close'] : '';
}
}
return $button;
}
}