<?php
002
003
004
005
006
007
008
009
010
011
012
013
014
class
minupage
015
{
016
017
018
019
var
$page_name
=
"p"
;
020
var
$next_page
=
'>'
;
021
var
$pre_page
=
'<'
;
022
var
$first_page
=
'First'
;
023
var
$last_page
=
'Last'
;
024
var
$pre_bar
=
'<<'
;
025
var
$next_bar
=
'>>'
;
026
var
$format_left
=
'['
;
027
var
$format_right
=
']'
;
028
var
$is_ajax
=false;
029
030
031
032
033
034
var
$pagebarnum
=10;
035
var
$totalpage
=0;
036
var
$ajax_action_name
=
''
;
037
var
$nowindex
=1;
038
var
$url
=
""
;
039
var
$offset
=0;
040
041
042
043
044
045
046
function
minupage(
$array
)
047
{
048
if
(
is_array
(
$array
)){
049
if
(!
array_key_exists
(
'total'
,
$array
))
$this
->error(
__FUNCTION__
,
'need a param of total'
);
050
$total
=
intval
(
$array
[
'total'
]);
051
$perpage
=(
array_key_exists
(
'perpage'
,
$array
))?
intval
(
$array
[
'perpage'
]):10;
052
$nowindex
=(
array_key_exists
(
'nowindex'
,
$array
))?
intval
(
$array
[
'nowindex'
]):
''
;
053
$url
=(
array_key_exists
(
'url'
,
$array
))?
$array
[
'url'
]:
''
;
054
}
else
{
055
$total
=
$array
;
056
$perpage
=10;
057
$nowindex
=
''
;
058
$url
=
''
;
059
}
060
if
((!
is_int
(
$total
))||(
$total
<0))
$this
->error(
__FUNCTION__
,
$total
.
' is not a positive integer!'
);
061
if
((!
is_int
(
$perpage
))||(
$perpage
<=0))
$this
->error(
__FUNCTION__
,
$perpage
.
' is not a positive integer!'
);
062
if
(!
empty
(
$array
[
'page_name'
]))
$this
->set(
'page_name'
,
$array
[
'page_name'
]);
063
$this
->_set_nowindex(
$nowindex
);
064
$this
->_set_url(
$url
);
065
$this
->totalpage=
ceil
(
$total
/
$perpage
);
066
$this
->offset=(
$this
->nowindex-1)*
$perpage
;
067
if
(!
empty
(
$array
[
'ajax'
]))
$this
->open_ajax(
$array
[
'ajax'
]);
068
}
069
070
071
072
073
074
075
function
set(
$var
,
$value
)
076
{
077
if
(in_array(
$var
,get_object_vars(
$this
)))
078
$this
->
$var
=
$value
;
079
else
{
080
$this
->error(
__FUNCTION__
,
$var
.
" does not belong to PB_Page!"
);
081
}
082
083
}
084
085
086
087
088
089
function
open_ajax(
$action
)
090
{
091
$this
->is_ajax=true;
092
$this
->ajax_action_name=
$action
;
093
}
094
095
096
097
098
099
100
function
next_page(
$style
=
''
)
101
{
102
if
(
$this
->nowindex<
$this
->totalpage){
103
return
$this
->_get_link(
$this
->_get_url(
$this
->nowindex+1),
$this
->next_page,
$style
);
104
}
105
return
'<span class="'
.
$style
.
'">'
.
$this
->next_page.
'</span>'
;
106
}
107
108
109
110
111
112
113
114
function
pre_page(
$style
=
''
)
115
{
116
if
(
$this
->nowindex>1){
117
return
$this
->_get_link(
$this
->_get_url(
$this
->nowindex-1),
$this
->pre_page,
$style
);
118
}
119
return
'<span class="'
.
$style
.
'">'
.
$this
->pre_page.
'</span>'
;
120
}
121
122
123
124
125
126
127
function
first_page(
$style
=
''
)
128
{
129
if
(
$this
->nowindex==1){
130
return
'<span class="'
.
$style
.
'">'
.
$this
->first_page.
'</span>'
;
131
}
132
return
$this
->_get_link(
$this
->_get_url(1),
$this
->first_page,
$style
);
133
}
134
135
136
137
138
139
140
function
last_page(
$style
=
''
)
141
{
142
if
(
$this
->nowindex==
$this
->totalpage){
143
return
'<span class="'
.
$style
.
'">'
.
$this
->last_page.
'</span>'
;
144
}
145
return
$this
->_get_link(
$this
->_get_url(
$this
->totalpage),
$this
->last_page,
$style
);
146
}
147
148
function
nowbar(
$style
=
''
,
$nowindex_style
=
''
)
149
{
150
$plus
=
ceil
(
$this
->pagebarnum/2);
151
if
(
$this
->pagebarnum-
$plus
+
$this
->nowindex>
$this
->totalpage)
$plus
=(
$this
->pagebarnum-
$this
->totalpage+
$this
->nowindex);
152
$begin
=
$this
->nowindex-
$plus
+1;
153
$begin
=(
$begin
>=1)?
$begin
:1;
154
$return
=
''
;
155
for
(
$i
=
$begin
;
$i
<
$begin
+
$this
->pagebarnum;
$i
++)
156
{
157
if
(
$i
<=
$this
->totalpage){
158
if
(
$i
!=
$this
->nowindex)
159
$return
.=
$this
->_get_text(
$this
->_get_link(
$this
->_get_url(
$i
),
$i
,
$style
));
160
else
161
$return
.=
$this
->_get_text(
'<span class="'
.
$nowindex_style
.
'">'
.
$i
.
'</span>'
);
162
}
else
{
163
break
;
164
}
165
$return
.=
"\n"
;
166
}
167
unset(
$begin
);
168
return
$return
;
169
}
170
171
172
173
174
175
function
select(
$url
)
176
{
177
$return
=
'<select name=";PB_Page_Select" >'
;
178
for
(
$i
=1;
$i
<=
$this
->totalpage;
$i
++)
179
{
180
if
(
$i
==
$this
->nowindex){
181
$return
.=
'<option value='
.
$url
.
$i
.
' selected>'
.
$i
.
'</option>'
;
182
}
else
{
183
$return
.=
'<option value='
.
$url
.
$i
.
'>'
.
$i
.
'</option>'
;
184
}
185
}
186
unset(
$i
);
187
$return
.=
'</select>'
;
188
return
$return
;
189
}
190
191
192
193
194
195
196
function
offset()
197
{
198
return
$this
->offset;
199
}
200
201
202
203
204
205
206
207
function
show(
$mode
=1,
$url
=
''
)
208
{
209
switch
(
$mode
)
210
{
211
case
'1'
:
212
$this
->next_page=
'下一页'
;
213
$this
->pre_page=
'上一页'
;
214
return
$this
->pre_page().
$this
->nowbar().
$this
->next_page().
'第'
.
$this
->select(
$url
).
'页'
;
215
break
;
216
case
'2'
:
217
$this
->next_page=
'下一页'
;
218
$this
->pre_page=
'上一页'
;
219
$this
->first_page=
'首页'
;
220
$this
->last_page=
'尾页'
;
221
return
$this
->first_page().
$this
->pre_page().
'[第'
.
$this
->nowindex.
' 页]'
.
$this
->next_page().
$this
->last_page().
'第 '
.
$this
->select(
$url
).
'页'
;
222
break
;
223
case
'3'
:
224
$this
->next_page=
'下一页'
;
225
$this
->pre_page=
'上一页'
;
226
$this
->first_page=
'首页'
;
227
$this
->last_page=
'尾页'
;
228
return
$this
->first_page().
$this
->pre_page().
$this
->next_page().
$this
->last_page();
229
break
;
230
case
'4'
:
231
$this
->next_page=
'下一页'
;
232
$this
->pre_page=
'上一页'
;
233
return
$this
->pre_page().
$this
->nowbar().
$this
->next_page();
234
break
;
235
case
'5'
:
236
return
$this
->pre_bar().
$this
->pre_page().
$this
->nowbar().
$this
->next_page().
$this
->next_bar();
237
break
;
238
}
239
240
}
241
242
243
244
245
246
247
function
_set_url(
$url
=
""
)
248
{
249
if
(!
empty
(
$url
)){
250
251
$this
->url=
$url
.((
stristr
(
$url
,
'?'
))?
'&'
:
'?'
).
$this
->page_name.
"="
;
252
}
else
{
253
254
if
(
empty
(
$_SERVER
[
'QUERY_STRING'
])){
255
256
$this
->url=
$_SERVER
[
'REQUEST_URI'
].
"?"
.
$this
->page_name.
"="
;
257
}
else
{
258
259
if
(
stristr
(
$_SERVER
[
'QUERY_STRING'
],
$this
->page_name.
'='
)){
260
261
$this
->url=
str_replace
(
$this
->page_name.
'='
.
$this
->nowindex,
''
,
$_SERVER
[
'REQUEST_URI'
]);
262
$last
=
$this
->url[
strlen
(
$this
->url)-1];
263
if
(
$last
==
'?'
||
$last
==
'&'
){
264
$this
->url.=
$this
->page_name.
"="
;
265
}
else
{
266
$this
->url.=
'&'
.
$this
->page_name.
"="
;
267
}
268
}
else
{
269
270
$this
->url=
$_SERVER
[
'REQUEST_URI'
].
'&'
.
$this
->page_name.
'='
;
271
}
272
}
273
}
274
}
275
276
277
278
279
280
function
_set_nowindex(
$nowindex
)
281
{
282
if
(
empty
(
$nowindex
)){
283
284
285
if
(isset(
$_GET
[
$this
->page_name])){
286
$this
->nowindex=
intval
(
$_GET
[
$this
->page_name]);
287
}
288
}
else
{
289
290
$this
->nowindex=
intval
(
$nowindex
);
291
}
292
}
293
294
295
296
297
298
299
300
function
_get_url(
$pageno
=1)
301
{
302
return
$this
->url.
$pageno
;
303
}
304
305
306
307
308
309
310
311
function
_get_text(
$str
)
312
{
313
return
$this
->format_left.
$str
.
$this
->format_right;
314
}
315
316
317
318
319
function
_get_link(
$url
,
$text
,
$style
=
''
){
320
$style
=(
empty
(
$style
))?
''
:
'class="'
.
$style
.
'"'
;
321
if
(
$this
->is_ajax){
322
323
return
'<a '
.
$style
.
' href="javascript:'
.
$this
->ajax_action_name.
'(\''
.
$url
.
'\')">'
.
$text
.
'</a>'
;
324
}
else
{
325
return
'<a '
.
$style
.
' href="'
.
$url
.
'">'
.
$text
.
'</a>'
;
326
}
327
}
328
329
330
331
function
error(
$function
,
$errormsg
)
332
{
333
die
(
'Error in file <b>'
.
__FILE__
.
'</b> ,Function <b>'
.
$function
.
'()</b> :'
.
$errormsg
);
334
}
335
}
336
?>