<?PHP |
@H_301_9@
002 |
/** |
@H_301_9@
003 |
演示 |
@H_301_9@
005 |
$page=new page(array('total'=>1000,'perpage'=>20)); |
@H_301_9@
006 |
echo 'mode:1<br>'.$page->show(); |
@H_301_9@
007 |
echo '<hr>mode:2<br>'.$page->show(2); |
@H_301_9@
008 |
echo '<hr>mode:3<br>'.$page->show(3); |
@H_301_9@
009 |
echo '<hr>mode:4<br>'.$page->show(4); |
@H_301_9@
010 |
echo '<hr>开始AJAX模式:'; |
@H_301_9@
011 |
$ajaxpage=new page(array('total'=>1000,'perpage'=>20,'ajax'=>'ajax_page','page_name'=>'test')); |
@H_301_9@
012 |
echo 'mode:1<br>'.$ajaxpage->show(); |
@H_301_9@
013 |
*/ |
@H_301_9@
014 |
class minupage |
@H_301_9@
015 |
{ |
@H_301_9@
016 |
/** |
@H_301_9@
017 |
* config,public |
@H_301_9@
018 |
*/ |
@H_301_9@
023 |
var $last_page = 'Last' ; //尾页 |
@H_301_9@
026 |
var $format_left = '[' ; |
@H_301_9@
027 |
var $format_right = ']' ; |
@H_301_9@
029 |
@H_301_9@ |
030 |
/** |
@H_301_9@
031 |
* private |
@H_301_9@
032 |
* |
@H_301_9@
033 |
*/ |
@H_301_9@
034 |
var $pagebarnum =10; //控制记录条的个数。 |
@H_301_9@
035 |
var $totalpage =0; //总页数 |
@H_301_9@
036 |
var $ajax_action_name = '' ; //AJAX动作名 |
@H_301_9@
037 |
var $nowindex =1; //当前页 |
@H_301_9@
039 |
var $offset =0; |
@H_301_9@
040 |
@H_301_9@ |
041 |
/** |
@H_301_9@
043 |
* |
@H_301_9@
044 |
* @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']... |
@H_301_9@
045 |
*/ |
@H_301_9@
046 |
function minupage( $array ) |
@H_301_9@
047 |
{ |
@H_301_9@
048 |
if ( is_array ( $array )){ |
@H_301_9@
049 |
if (! array_key_exists ( 'total' , $array )) $this ->error( __FUNCTION__ , 'need a param of total' ); |
@H_301_9@
050 |
$total = intval ( $array [ 'total' ]); |
@H_301_9@
051 |
$perpage =( array_key_exists ( 'perpage' , $array ))? intval ( $array [ 'perpage' ]):10; |
@H_301_9@
052 |
$nowindex =( array_key_exists ( 'nowindex' , $array ))? intval ( $array [ 'nowindex' ]): '' ; |
@H_301_9@
053 |
$url =( array_key_exists ( 'url' , $array ))? $array [ 'url' ]: '' ; |
@H_301_9@
054 |
} else { |
@H_301_9@
055 |
$total = $array ; |
@H_301_9@
056 |
$perpage =10; |
@H_301_9@
057 |
$nowindex = '' ; |
@H_301_9@
058 |
$url = '' ; |
@H_301_9@
059 |
} |
@H_301_9@
060 |
if ((! is_int ( $total ))||( $total <0)) $this ->error( __FUNCTION__ , $total . ' is not a positive integer!' ); |
@H_301_9@
061 |
if ((! is_int ( $perpage ))||( $perpage <=0)) $this ->error( __FUNCTION__ , $perpage . ' is not a positive integer!' ); |
@H_301_9@
062 |
if (! empty ( $array [ 'page_name' ])) $this ->set( 'page_name' , $array [ 'page_name' ]); //设置pagename |
@H_301_9@
063 |
$this ->_set_nowindex( $nowindex ); //设置当前页 |
@H_301_9@
065 |
$this ->totalpage= ceil ( $total / $perpage ); |
@H_301_9@
066 |
$this ->offset=( $this ->nowindex-1)* $perpage ; |
@H_301_9@
067 |
if (! empty ( $array [ 'ajax' ])) $this ->open_ajax( $array [ 'ajax' ]); //打开AJAX模式 |
@H_301_9@
068 |
} |
@H_301_9@
069 |
/** |
@H_301_9@
070 |
* 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception |
@H_301_9@
071 |
* |
@H_301_9@
072 |
* @param string $var |
@H_301_9@
073 |
* @param string $value |
@H_301_9@
074 |
*/ |
@H_301_9@
075 |
function set( $var , $value ) |
@H_301_9@
076 |
{ |
@H_301_9@
077 |
if (in_array( $var ,get_object_vars( $this ))) |
@H_301_9@
078 |
$this -> $var = $value ; |
@H_301_9@
079 |
else { |
@H_301_9@
080 |
$this ->error( __FUNCTION__ , $var . " does not belong to PB_Page!" ); |
@H_301_9@
081 |
} |
@H_301_9@
082 |
@H_301_9@ |
083 |
} |
@H_301_9@
084 |
/** |
@H_301_9@
085 |
* 打开倒AJAX模式 |
@H_301_9@
086 |
* |
@H_301_9@
087 |
* @param string $action 默认ajax触发的动作。 |
@H_301_9@
088 |
*/ |
@H_301_9@
089 |
function open_ajax( $action ) |
@H_301_9@
090 |
{ |
@H_301_9@
091 |
$this ->is_ajax=true; |
@H_301_9@
092 |
$this ->ajax_action_name= $action ; |
@H_301_9@
093 |
} |
@H_301_9@
094 |
/** |
@H_301_9@
096 |
* |
@H_301_9@
097 |
* @param string $style |
@H_301_9@
098 |
* @return string |
@H_301_9@
099 |
*/ |
@H_301_9@
100 |
function next_page( $style = '' ) |
@H_301_9@
101 |
{ |
@H_301_9@
102 |
if ( $this ->nowindex< $this ->totalpage){ |
@H_301_9@
103 |
return $this ->_get_link( $this ->_get_url( $this ->nowindex+1), $this ->next_page, $style ); |
@H_301_9@
104 |
} |
@H_301_9@
105 |
return '<span class="' . $style . '">' . $this ->next_page. '</span>' ; |
@H_301_9@
106 |
} |
@H_301_9@
107 |
@H_301_9@ |
108 |
/** |
@H_301_9@
110 |
* |
@H_301_9@
111 |
* @param string $style |
@H_301_9@
112 |
* @return string |
@H_301_9@
113 |
*/ |
@H_301_9@
114 |
function pre_page( $style = '' ) |
@H_301_9@
115 |
{ |
@H_301_9@
116 |
if ( $this ->nowindex>1){ |
@H_301_9@
117 |
return $this ->_get_link( $this ->_get_url( $this ->nowindex-1), $this ->pre_page, $style ); |
@H_301_9@
118 |
} |
@H_301_9@
119 |
return '<span class="' . $style . '">' . $this ->pre_page. '</span>' ; |
@H_301_9@
120 |
} |
@H_301_9@
121 |
@H_301_9@ |
122 |
/** |
@H_301_9@
124 |
* |
@H_301_9@
125 |
* @return string |
@H_301_9@
126 |
*/ |
@H_301_9@
127 |
function first_page( $style = '' ) |
@H_301_9@
128 |
{ |
@H_301_9@
129 |
if ( $this ->nowindex==1){ |
@H_301_9@
130 |
return '<span class="' . $style . '">' . $this ->first_page. '</span>' ; |
@H_301_9@
131 |
} |
@H_301_9@
132 |
return $this ->_get_link( $this ->_get_url(1), $this ->first_page, $style ); |
@H_301_9@
133 |
} |
@H_301_9@
134 |
@H_301_9@ |
135 |
/** |
@H_301_9@
137 |
* |
@H_301_9@
138 |
* @return string |
@H_301_9@
139 |
*/ |
@H_301_9@
140 |
function last_page( $style = '' ) |
@H_301_9@
141 |
{ |
@H_301_9@
142 |
if ( $this ->nowindex== $this ->totalpage){ |
@H_301_9@
143 |
return '<span class="' . $style . '">' . $this ->last_page. '</span>' ; |
@H_301_9@
144 |
} |
@H_301_9@
145 |
return $this ->_get_link( $this ->_get_url( $this ->totalpage), $this ->last_page, $style ); |
@H_301_9@
146 |
} |
@H_301_9@
147 |
@H_301_9@ |
148 |
function nowbar( $style = '' , $nowindex_style = '' ) |
@H_301_9@
149 |
{ |
@H_301_9@
150 |
$plus = ceil ( $this ->pagebarnum/2); |
@H_301_9@
151 |
if ( $this ->pagebarnum- $plus + $this ->nowindex> $this ->totalpage) $plus =( $this ->pagebarnum- $this ->totalpage+ $this ->nowindex); |
@H_301_9@
152 |
$begin = $this ->nowindex- $plus +1; |
@H_301_9@
153 |
$begin =( $begin >=1)? $begin :1; |
@H_301_9@
154 |
$return = '' ; |
@H_301_9@
155 |
for ( $i = $begin ; $i < $begin + $this ->pagebarnum; $i ++) |
@H_301_9@
156 |
{ |
@H_301_9@
157 |
if ( $i <= $this ->totalpage){ |
@H_301_9@
158 |
if ( $i != $this ->nowindex) |
@H_301_9@
159 |
$return .= $this ->_get_text( $this ->_get_link( $this ->_get_url( $i ), $i , $style )); |
@H_301_9@
160 |
else |
@H_301_9@
161 |
$return .= $this ->_get_text( '<span class="' . $nowindex_style . '">' . $i . '</span>' ); |
@H_301_9@
162 |
} else { |
@H_301_9@
163 |
break ; |
@H_301_9@
164 |
} |
@H_301_9@
165 |
$return .= "\n" ; |
@H_301_9@
166 |
} |
@H_301_9@
167 |
unset( $begin ); |
@H_301_9@
168 |
return $return ; |
@H_301_9@
169 |
} |
@H_301_9@
170 |
/** |
@H_301_9@
172 |
* |
@H_301_9@
173 |
* @return string |
@H_301_9@
174 |
*/ |
@H_301_9@
175 |
function select( $url ) |
@H_301_9@
176 |
{ |
@H_301_9@
177 |
$return = '<select name=";PB_Page_Select" >' ; |
@H_301_9@
178 |
for ( $i =1; $i <= $this ->totalpage; $i ++) |
@H_301_9@
179 |
{ |
@H_301_9@
180 |
if ( $i == $this ->nowindex){ |
@H_301_9@
181 |
$return .= '<option value=' . $url . $i . ' selected>' . $i . '</option>' ; |
@H_301_9@
182 |
} else { |
@H_301_9@
183 |
$return .= '<option value=' . $url . $i . '>' . $i . '</option>' ; |
@H_301_9@
184 |
} |
@H_301_9@
185 |
} |
@H_301_9@
186 |
unset( $i ); |
@H_301_9@
187 |
$return .= '</select>' ; |
@H_301_9@
188 |
return $return ; |
@H_301_9@
189 |
} |
@H_301_9@
190 |
@H_301_9@ |
191 |
/** |
@H_301_9@
193 |
* |
@H_301_9@
194 |
* @return string |
@H_301_9@
195 |
*/ |
@H_301_9@
196 |
function offset() |
@H_301_9@
197 |
{ |
@H_301_9@
198 |
return $this ->offset; |
@H_301_9@
199 |
} |
@H_301_9@
200 |
@H_301_9@ |
201 |
/** |
@H_301_9@
203 |
* |
@H_301_9@
204 |
* @param int $mode |
@H_301_9@
205 |
* @return string |
@H_301_9@
206 |
*/ |
@H_301_9@
207 |
function show( $mode =1, $url = '' ) |
@H_301_9@
208 |
{ |
@H_301_9@
209 |
switch ( $mode ) |
@H_301_9@
210 |
{ |
@H_301_9@
211 |
case '1' : |
@H_301_9@
214 |
return $this ->pre_page(). $this ->nowbar(). $this ->next_page(). '第' . $this ->select( $url ). '页' ; |
@H_301_9@
215 |
break ; |
@H_301_9@
216 |
case '2' : |
@H_301_9@
220 |
$this ->last_page= '尾页' ; |
@H_301_9@
221 |
return $this ->first_page(). $this ->pre_page(). '[第' . $this ->nowindex. ' 页]' . $this ->next_page(). $this ->last_page(). '第 ' . $this ->select( $url ). '页' ; |
@H_301_9@
222 |
break ; |
@H_301_9@
223 |
case '3' : |
@H_301_9@
227 |
$this ->last_page= '尾页' ; |
@H_301_9@
228 |
return $this ->first_page(). $this ->pre_page(). $this ->next_page(). $this ->last_page(); |
@H_301_9@
229 |
break ; |
@H_301_9@
230 |
case '4' : |
@H_301_9@
233 |
return $this ->pre_page(). $this ->nowbar(). $this ->next_page(); |
@H_301_9@
234 |
break ; |
@H_301_9@
235 |
case '5' : |
@H_301_9@
236 |
return $this ->pre_bar(). $this ->pre_page(). $this ->nowbar(). $this ->next_page(). $this ->next_bar(); |
@H_301_9@
237 |
break ; |
@H_301_9@
238 |
} |
@H_301_9@
239 |
@H_301_9@ |
240 |
} |
@H_301_9@
241 |
/*----------------private function (私有方法)-----------------------------------------------------------*/ |
@H_301_9@
242 |
/** |
@H_301_9@
243 |
* 设置url头地址 |
@H_301_9@
244 |
* @param: String $url |
@H_301_9@
245 |
* @return boolean |
@H_301_9@
246 |
*/ |
@H_301_9@
247 |
function _set_url( $url = "" ) |
@H_301_9@
248 |
{ |
@H_301_9@
249 |
if (! empty ( $url )){ |
@H_301_9@
250 |
//手动设置 |
@H_301_9@
251 |
$this ->url= $url .(( stristr ( $url , '?' ))? '&' : '?' ). $this ->page_name. "=" ; |
@H_301_9@
252 |
} else { |
@H_301_9@
254 |
if ( empty ( $_SERVER [ 'QUERY_STRING' ])){ |
@H_301_9@
255 |
//不存在QUERY_STRING时 |
@H_301_9@
256 |
$this ->url= $_SERVER [ 'REQUEST_URI' ]. "?" . $this ->page_name. "=" ; |
@H_301_9@
257 |
} else { |
@H_301_9@
258 |
// |
@H_301_9@
259 |
if ( stristr ( $_SERVER [ 'QUERY_STRING' ], $this ->page_name. '=' )){ |
@H_301_9@
261 |
$this ->url= str_replace ( $this ->page_name. '=' . $this ->nowindex, '' , $_SERVER [ 'REQUEST_URI' ]); |
@H_301_9@
262 |
$last = $this ->url[ strlen ( $this ->url)-1]; |
@H_301_9@
263 |
if ( $last == '?' || $last == '&' ){ |
@H_301_9@
264 |
$this ->url.= $this ->page_name. "=" ; |
@H_301_9@
265 |
} else { |
@H_301_9@
266 |
$this ->url.= '&' . $this ->page_name. "=" ; |
@H_301_9@
267 |
} |
@H_301_9@
268 |
} else { |
@H_301_9@
269 |
// |
@H_301_9@
270 |
$this ->url= $_SERVER [ 'REQUEST_URI' ]. '&' . $this ->page_name. '=' ; |
@H_301_9@
271 |
} //end if |
@H_301_9@
272 |
} //end if |
@H_301_9@
273 |
} //end if |
@H_301_9@
274 |
} |
@H_301_9@
275 |
@H_301_9@ |
276 |
/** |
@H_301_9@
278 |
* |
@H_301_9@
279 |
*/ |
@H_301_9@
280 |
function _set_nowindex( $nowindex ) |
@H_301_9@
281 |
{ |
@H_301_9@
282 |
if ( empty ( $nowindex )){ |
@H_301_9@
284 |
|
@H_301_9@
285 |
if (isset( $_GET [ $this ->page_name])){ |
@H_301_9@
286 |
$this ->nowindex= intval ( $_GET [ $this ->page_name]); |
@H_301_9@
287 |
} |
@H_301_9@
288 |
} else { |
@H_301_9@
289 |
//手动设置 |
@H_301_9@
290 |
$this ->nowindex= intval ( $nowindex ); |
@H_301_9@
291 |
} |
@H_301_9@
292 |
} |
@H_301_9@
293 |
@H_301_9@ |
294 |
/** |
@H_301_9@
296 |
* |
@H_301_9@
297 |
* @param int $pageno |
@H_301_9@
298 |
* @return string $url |
@H_301_9@
299 |
*/ |
@H_301_9@
300 |
function _get_url( $pageno =1) |
@H_301_9@
302 |
return $this ->url. $pageno ; |
@H_301_9@
303 |
} |
@H_301_9@
304 |
@H_301_9@ |
305 |
/** |
@H_301_9@
307 |
* |
@H_301_9@
308 |
* @param String $str |
@H_301_9@
309 |
* @return string $url |
@H_301_9@
310 |
*/ |
@H_301_9@
311 |
function _get_text( $str ) |
@H_301_9@
312 |
{ |
@H_301_9@
313 |
return $this ->format_left. $str . $this ->format_right; |
@H_301_9@
314 |
} |
@H_301_9@
315 |
@H_301_9@ |
316 |
/** |
@H_301_9@
318 |
*/ |
@H_301_9@
319 |
function _get_link( $url , $text , $style = '' ){ |
@H_301_9@
320 |
$style =( empty ( $style ))? '' : 'class="' . $style . '"' ; |
@H_301_9@
321 |
if ( $this ->is_ajax){ |
@H_301_9@
322 |
//如果是使用AJAX模式 |
@H_301_9@
323 |
return '<a ' . $style . ' href="javascript:' . $this ->ajax_action_name. '(\'' . $url . '\')">' . $text . '</a>' ; |
@H_301_9@
324 |
} else { |
@H_301_9@
325 |
return '<a ' . $style . ' href="' . $url . '">' . $text . '</a>' ; |
@H_301_9@
326 |
} |
@H_301_9@
327 |
} |
@H_301_9@
328 |
/** |
@H_301_9@
329 |
* 出错处理方式 |
@H_301_9@
330 |
*/ |
@H_301_9@
331 |
function error( $function , $errormsg ) |
@H_301_9@
332 |
{ |
@H_301_9@
333 |
die ( 'Error in file <b>' . __FILE__ . '</b>,Function <b>' . $function . '()</b> :' . $errormsg ); |
@H_301_9@
334 |
} |
@H_301_9@
335 |
} |
@H_301_9@
336 |
?> |
@H_301_9@