<?php |
002 | /** |
003 | 演示 |
004 | require_once('../libs/classes/page.class.php'); |
005 | $page=new page(array('total'=>1000,'perpage'=>20)); |
006 | echo 'mode:1<br>'.$page->show(); |
007 | echo '<hr>mode:2<br>'.$page->show(2); |
008 | echo '<hr>mode:3<br>'.$page->show(3); |
009 | echo '<hr>mode:4<br>'.$page->show(4); |
010 | echo '<hr>开始AJAX模式:'; |
011 | $ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test')); |
012 | echo 'mode:1<br>'.$ajaxpage->show(); |
013 | */ |
014 | class
minupage |
015 | { |
016 | /** |
017 | * config ,public |
018 | */ |
019 | var
$page_name = "p" ; //page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page |
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; //是否支持AJAX分页模式 |
029 |
030 | /** |
031 | * private |
032 | * |
033 | */ |
034 | var
$pagebarnum =10; //控制记录条的个数。 |
035 | var
$totalpage =0; //总页数 |
036 | var
$ajax_action_name = '' ; //AJAX动作名 |
037 | var
$nowindex =1; //当前页 |
038 | var
$url = "" ; //url地址头 |
039 | var
$offset =0; |
040 |
041 | /** |
042 | * constructor构造函数 |
043 | * |
044 | * @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']... |
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' ]); //设置pagename |
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' ]); //打开AJAX模式 |
068 | } |
069 | /** |
070 | * 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception |
071 | * |
072 | * @param string $var |
073 | * @param string $value |
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 | * 打开倒AJAX模式 |
086 | * |
087 | * @param string $action 默认ajax触发的动作。 |
088 | */ |
089 | function
open_ajax( $action ) |
090 | { |
091 | $this ->is_ajax=true; |
092 | $this ->ajax_action_name= $action ; |
093 | } |
094 | /** |
095 | * 获取显示"下一页"的代码 |
096 | * |
097 | * @param string $style |
098 | * @return string |
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 | * @param string $style |
112 | * @return string |
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 | * @return string |
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 | * @return string |
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 | * @return string |
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 | * 获取mysql 语句中limit需要的值 |
193 | * |
194 | * @return string |
195 | */ |
196 | function
offset() |
197 | { |
198 | return
$this ->offset; |
199 | } |
200 |
201 | /** |
202 | * 控制分页显示风格(你可以增加相应的风格) |
203 | * |
204 | * @param int $mode |
205 | * @return string |
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 | /*----------------private function (私有方法)-----------------------------------------------------------*/ |
242 | /** |
243 | * 设置url头地址 |
244 | * @param: String $url |
245 | * @return boolean |
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 |
//不存在QUERY_STRING时 |
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 |
} //end if |
272 | } //end if |
273 | } //end if |
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 | * @param int $pageno |
298 | * @return string $url |
299 | */ |
300 | function
_get_url( $pageno =1) |
301 | { |
302 | return
$this ->url. $pageno ; |
303 | } |
304 |
305 | /** |
306 | * 获取分页显示文字,比如说默认情况下_get_text('<a href="">1</a>')将返回[<a href="">1</a>] |
307 | * |
308 | * @param String $str |
309 | * @return string $url |
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 |
//如果是使用AJAX模式 |
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 | ?> |