1//用法1: 2echo Page::instance(32,10,5,5,'show?p=','&lang=zh')->show(); 3 4//用法2: 5echo Page::instance()->show(32,10,5,5,'show?p=','&lang=zh'); 6 7//用法3: 8$page = Page::instance(); 9$page->set_config('theme', ' %first% %up% %prev% %page% %next% %down% %last% '); 10echo $page->show(32,10,5,5,'show?p=','&lang=zh'); 11 12//用法4: 13$page = Page::instance(32,10,5,5,'show?p=','&lang=zh'); 14echo $page->get('page_theme'); 15echo $page->get('next');//获取下一页,这里显示有设置的主题 16 17//用法5: 18$page = Page::instance(); 19$page->set(32,10,5,5,'show?p=','&lang=zh'); 20echo $page->get('page_theme'); 21echo $page->get('next');//获取下一页,这里显示有设置的主题 22 23//注:该类不能对已设数据进行覆盖,比如32,10这些在同一次的执行中不能被其他数据覆盖。如果要进行覆盖,可将已设数据更改为NULL 24 25 26 27 28 29<?php 30 31 32 33/* 34 * 35 * @ID: 分页类 36 * 37 * @auther: 欣儿 38 * 39 * @time: 2014/02/20 40 * 41 * @web: http://my.oschina.net/xinger 42 * 43**/ 44 45 46 47class Page { 48 49 public $left = '%'; 50 public $right = '%'; 51 public $first = 1 ; 52 53 public $count; 54 public $perlogs; 55 public $page; 56 public $roll_page; 57 public $url; 58 public $suffix; 59 60 public $total;//总页数 61 public $prev;//上一页 62 public $next;//下一页 63 public $up;//上大翻页 64 public $down;//下大翻页 65 66 protected $now_page; 67 protected $other_page; 68 protected $prev_page; 69 protected $next_page; 70 protected $up_page; 71 protected $down_page; 72 protected $first_page; 73 protected $last_page; 74 75 protected $var = array();//暂存数据 76 protected $config = array( 77 'other_theme'=>'<span class="current"><a href="%page_url%">%i%</a></span>',//页面页 78 'now_theme'=>'<span class="current_show"><a href="%now_url%">%i%</a></span>',//当前页 %page%为other和now的组合 79 80 'prev_theme'=>'<span class="current_pre"><a href="%prev_url%" title="上一页"><上一页</a></span>',//url可在theme单独使用 81 'next_theme'=>'<span class="current_next"><a href="%next_url%" title="下一页">下一页></a></span>', 82 'up_theme'=>'<span class="current_up"><a href="%up_url%" title="<<"><<</a></span>',// 83 'down_theme'=>'<span class="current_down"><a href="%down_url%" title=">>">>></a></span>', 84 'first_theme'=>'<span class="current_start"><a href="%first_url%" title="首页">首页</a></span>', 85 'last_theme'=>'<span class="current_end"><a href="%last_url%" title="尾页">尾页</a></span>', 86 87 'prev_end_theme'=>'',//不显示的时候总是显示时显示的主题 88 'next_end_theme'=>'', 89 'up_end_theme'=>'',// 90 'down_end_theme'=>'', 91 'first_end_theme'=>'', 92 'last_end_theme'=>'', 93 94 'theme'=>' %first% %up% %prev% %page% %next% %down% %last% ',//显示样式 共有 %count% 条数据, 第 %now_page% / %total% 页 %prev_always% %up_always% %down_always% 95 ); 96 97 public function __construct($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) { 98 $this->set($count, $perlogs, $now_page, $roll_page, $url, $suffix); 99 } 100 101 public static function instance($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) { 102 static $var; 103 if(isset($var)) return $var; 104 105 $var = new self($count, $perlogs, $now_page, $roll_page, $url, $suffix); 106 return $var; 107 } 108 //显示 109 public function show($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) { 110 $this->set($count, $perlogs, $now_page, $roll_page, $url, $suffix); 111 $this->compile(); 112 113 $page = $this->var['page_theme']; 114 return $page; 115 116 } 117 //获取返回信息 118 public function get($key = NULL) { 119 $this->compile(); 120 if(NULL !== $key && isset($this->var[$key])) return $this->var[$key]; 121 return $this->var; 122 } 123 //设置: 条目总数, 每页显示条数目, 当前页码, 当前页码的前后页数, 页码的地址, 地址其他项 124 public function set($count = NULL, $perlogs = NULL, $now_page = NULL, $roll_page = NULL, $url = NULL, $suffix = NULL) { 125 if (NULL === $this->count) $this->count = $this->var['count'] = $count; 126 if (NULL === $this->perlogs) $this->perlogs = $this->var['perlogs'] = $perlogs; 127 if (NULL === $this->page) $this->page = $this->var['now_page'] = $now_page; 128 if (NULL === $this->roll_page) $this->roll_page = $this->var['roll_page'] = $roll_page;//与当前页左右相距的页数 129 if (NULL === $this->url) $this->url = $this->var['url'] = $url; 130 if (NULL === $this->suffix) $this->suffix = $this->var['suffix'] = $suffix; 131 } 132 133 public function set_config($name, $value = NULL) { 134 if (isset($this->config[$name])) { 135 $this->config[$name] = $value; 136 return true; 137 } 138 return false; 139 } 140 141 public function get_config($name = NULL) { 142 if (NULL === $name) return $this->config; 143 144 if (isset($this->config[$name])) { 145 return $this->config[$name]; 146 } 147 148 return false; 149 } 150 151 public function get_count() { 152 return $this->count; 153 } 154 155 public function get_perlogs() { 156 return $this->perlogs; 157 } 158 159 public function get_page() { 160 return $this->page; 161 } 162 163 public function get_roll_page() { 164 return $this->roll_page; 165 } 166 167 public function get_url() { 168 return $this->url; 169 } 170 171 public function get_suffix() { 172 return $this->suffix; 173 } 174 175 public function get_first() { 176 return $this->first; 177 } 178 179 protected function get_total() { 180 $this->total = @ceil($this->count / $this->perlogs); 181 return $this->total; 182 } 183 184 protected function get_prev() { 185 $this->prev = $this->page - 1; 186 return $this->prev; 187 } 188 189 protected function get_next() { 190 $this->next = $this->page + 1; 191 return $this->next; 192 } 193 194 protected function get_up($i = 5) { 195 $this->up = $this->page - $i; 196 return $this->up; 197 } 198 199 protected function get_down($i = 5) { 200 $this->down = $this->page + $i; 201 return $this->down; 202 } 203 204 protected function get_prev_page($always = false) { 205 if ($always) $this->prev += 1; 206 $this->prev_page = $this->url . $this->prev . $this->suffix; 207 return $this->prev_page; 208 } 209 210 protected function get_next_page($always = false) { 211 if ($always) $this->next -= 1; 212 $this->next_page = $this->url . $this->next . $this->suffix; 213 return $this->next_page; 214 } 215 216 protected function get_up_page($always = false) { 217 $roll_page = $this->get_roll_page(); 218 $now_page = $this->get_page(); 219 $first = $this->get_first(); 220 221 if ($always) { 222 if ($now_page <= $roll_page * 2 && $now_page > $roll_page) { 223 $this->up += $roll_page; 224 } else { 225 $this->up = $first; 226 } 227 } 228 $this->up_page = $this->url . $this->up . $this->suffix; 229 return $this->up_page; 230 } 231 232 protected function get_down_page($always = false) { 233 $total = $this->get_total(); 234 $roll_page = $this->get_roll_page(); 235 $now_page = $this->get_page(); 236 237 if ($always) { 238 $page = $total - $now_page; 239 if ($page <= $roll_page * 2 && $page >= $roll_page) { 240 $this->down -= $roll_page; 241 } else { 242 $this->down = $total; 243 } 244 } 245 $this->down_page = $this->url . $this->down . $this->suffix; 246 return $this->down_page; 247 } 248 249 protected function get_first_page() { 250 $this->first_page = $this->url . $this->first . $this->suffix; 251 return $this->first_page; 252 } 253 254 protected function get_last_page() { 255 $this->last_page = $this->url . $this->last . $this->suffix; 256 return $this->last_page; 257 } 258 259 protected function get_now_page($i = NULL) { 260 $this->now_page = $this->url . $i . $this->suffix; 261 return $this->now_page; 262 } 263 264 protected function get_other_page($i = NULL) { 265 $this->other_page = $this->url . $i . $this->suffix; 266 return $this->other_page; 267 } 268 269 protected function get_prev_theme($always = false) { 270 $total = $this->get_total(); 271 $now_page = $this->get_page(); 272 $prev = $this->get_prev(); 273 $prev_page = ($always === true) ? $this->get_prev_page(true) : $this->get_prev_page(); 274 275 $prev_always = NULL; 276 277 if($prev > 0 && $now_page <= $total) { 278 $prev_always = strtr($this->config['prev_theme'], array($this->left . 'prev_url' . $this->right => $prev_page)); 279 } 280 281 if($prev <= 0 && $always) { 282 if(!empty($this->config['prev_end_theme'])) { 283 $prev_theme = $this->config['prev_end_theme']; 284 } else { 285 $prev_theme = $this->config['prev_theme']; 286 } 287 $prev_always = strtr($prev_theme, array($this->left . 'prev_url' . $this->right => $prev_page)); 288 } 289 290 return $prev_always; 291 } 292 293 protected function get_next_theme($always = false) { 294 $next = $this->get_next(); 295 $total = $this->get_total(); 296 $next_page = ($always === true) ? $this->get_next_page(true) : $this->get_next_page(); 297 298 $next_always = NULL; 299 300 if($next < $total + 1){ 301 $next_always = strtr($this->config['next_theme'], array($this->left . 'next_url' . $this->right => $next_page)); 302 } 303 304 if($next >= $total + 1 && $always){ 305 if(!empty($this->config['next_end_theme'])) { 306 $next_theme = $this->config['next_end_theme']; 307 } else { 308 $next_theme = $this->config['next_theme']; 309 } 310 $next_always = strtr($next_theme, array($this->left . 'next_url' . $this->right => $next_page)); 311 } 312 313 return $next_always; 314 } 315 316 protected function get_up_theme($always = false) { 317 $total = $this->get_total(); 318 $roll_page = $this->get_roll_page(); 319 $now_page = $this->get_page(); 320 $roll_page = $roll_page * 2; 321 $up = $this->get_up($roll_page); 322 $up_page = ($always === true) ? $this->get_up_page(true) : $this->get_up_page(); 323 324 $up_always = NULL; 325 326 if($up > 0 && $now_page <= $total){ 327 $up_always = strtr($this->config['up_theme'], array($this->left . 'up_url' . $this->right => $up_page)); 328 } 329 330 if($up <= 0 && $always){ 331 if(!empty($this->config['up_end_theme'])){ 332 $up_theme = $this->config['up_end_theme']; 333 } else { 334 $up_theme = $this->config['up_theme']; 335 } 336 $up_always = strtr($up_theme, array($this->left . 'up_url' . $this->right => $up_page)); 337 } 338 339 return $up_always; 340 } 341 342 protected function get_down_theme($always = false) { 343 $total = $this->get_total(); 344 $roll_page = $this->get_roll_page(); 345 $roll_page = $roll_page * 2; 346 $down = $this->get_down($roll_page); 347 $down_page = ($always === true) ? $this->get_down_page(true) : $this->get_down_page(); 348 349 $down_always = NULL; 350 351 if($down < $total + 1){ 352 $down_always = strtr($this->config['down_theme'], array($this->left . 'down_url' . $this->right => $down_page)); 353 } 354 355 if($down >= $total + 1 && $always){ 356 if(!empty($this->config['down_end_theme'])){ 357 $down_theme = $this->config['down_end_theme']; 358 } else { 359 $down_theme = $this->config['down_theme']; 360 } 361 $down_always = strtr($down_theme, array($this->left . 'down_url' . $this->right => $down_page)); 362 } 363 364 return $down_always; 365 } 366 367 protected function get_page_theme($always = false) { 368 $total = $this->get_total(); 369 $now_page = $this->get_page(); 370 $roll_page = $this->get_roll_page(); 371 372 $page = NULL; 373 374 for ($i = $now_page - $roll_page;$i <= $now_page + $roll_page && $i <= $total; $i++){ 375 if ($i > 0){ 376 if ($i == $now_page) { 377 $page .= strtr($this->config['now_theme'], array($this->left . 'page_url' . $this->right => $this->get_now_page($i),$this->left . 'now_url' . $this->right => $this->get_other_page($i),$this->left . 'i' . $this->right => $i)); 378 } else { 379 $page .= strtr($this->config['other_theme'], array($this->left . 'page_url' . $this->right => $this->get_now_page($i),$this->left . 'now_url' . $this->right => $this->get_other_page($i),$this->left . 'i' . $this->right => $i)); 380 } 381 } 382 } 383 384 if ($total <= 1 && $always == false) $page = ''; 385 386 return $page; 387 } 388 389 protected function get_first_theme($always = false) { 390 $total = $this->get_total(); 391 $now_page = $this->get_page(); 392 $roll_page = $this->get_roll_page(); 393 $first_page = $this->get_first_page(); 394 395 $first_always = NULL; 396 397 if ($now_page > $roll_page + 1 && $now_page <= $total) { 398 $first_always = strtr($this->config['first_theme'], array($this->left . 'first_url' . $this->right => $first_page)); 399 } 400 401 if ($now_page <= $roll_page + 1 && $always) { 402 if(!empty($this->config['first_end_theme'])) { 403 $first_theme = $this->config['first_end_theme']; 404 } else { 405 $first_theme = $this->config['first_theme']; 406 } 407 $first_always = strtr($first_theme, array($this->left . 'first_url' . $this->right => $first_page)); 408 } 409 410 return $first_always; 411 } 412 413 protected function get_last_theme($always = false) { 414 $now_page = $this->get_page(); 415 $total = $this->get_total(); 416 $roll_page = $this->get_roll_page(); 417 $last_page = $this->get_last_page(); 418 419 $last_always = NULL; 420 421 if ($now_page + $roll_page < $total || $always) { 422 $last_always = strtr($this->config['last_theme'], array($this->left . 'last_url' . $this->right => $last_page)); 423 } 424 425 if ($now_page + $roll_page >= $total && $always) { 426 if(!empty($this->config['last_end_theme'])){ 427 $last_theme = $this->config['last_end_theme']; 428 } else { 429 $last_theme = $this->config['last_theme']; 430 } 431 $last_always = strtr($last_theme, array($this->left . 'last_url' . $this->right => $last_page)); 432 } 433 434 return $last_always; 435 } 436 //compile 437 protected function compile() { 438 $count = $this->get_count(); 439 $now_page = $this->get_page(); 440 $roll_page = $this->get_roll_page(); 441 442 $total = $this->var['total'] = $this->last = $this->get_total(); 443 $prev_url = $this->var['prev_url'] = $this->get_prev_page(); 444 $next_url = $this->var['next_url'] = $this->get_next_page(); 445 $first_url = $this->var['first_url'] = $this->get_first_page(); 446 $last_url = $this->var['last_url'] = $this->get_last_page(); 447 448 $prev = $this->var['prev'] = $this->get_prev_theme(); 449 $next = $this->var['next'] = $this->get_next_theme(); 450 $page = $this->var['page'] = $this->get_page_theme(); 451 $up = $this->var['up'] = $this->get_up_theme(); 452 $down = $this->var['down'] = $this->get_down_theme(); 453 $first = $this->var['first'] = $this->get_first_theme(); 454 $last = $this->var['last'] = $this->get_last_theme(); 455 456 $prev_always = $this->var['prev_always'] = $this->get_prev_theme(true); 457 $next_always = $this->var['next_always'] = $this->get_next_theme(true); 458 $page_always = $this->var['page_always'] = $this->get_page_theme(true); 459 $up_always = $this->var['up_always'] = $this->get_up_theme(true); 460 $down_always = $this->var['down_always'] = $this->get_down_theme(true); 461 $first_always = $this->var['first_always'] = $this->get_first_theme(true); 462 $last_always = $this->var['last_always'] = $this->get_last_theme(true); 463 464 $theme_parse = array( 465 $this->left . 'first' . $this->right => $first, 466 $this->left . 'up' . $this->right => $up, 467 $this->left . 'prev' . $this->right => $prev, 468 $this->left . 'page' . $this->right => $page, 469 $this->left . 'next' . $this->right => $next, 470 $this->left . 'down' . $this->right => $down, 471 $this->left . 'last' . $this->right => $last, 472 473 $this->left . 'first_always' . $this->right => $first_always, 474 $this->left . 'up_always' . $this->right => $up_always, 475 $this->left . 'prev_always' . $this->right => $prev_always, 476 $this->left . 'page_always' . $this->right => $page_always, 477 $this->left . 'next_always' . $this->right => $next_always, 478 $this->left . 'down_always' . $this->right => $down_always, 479 $this->left . 'last_always' . $this->right => $last_always, 480 481 $this->left . 'total' . $this->right => $total, 482 $this->left . 'count' . $this->right => $count, 483 $this->left . 'now_page' . $this->right => $now_page, 484 $this->left . 'roll_page' . $this->right => $roll_page, 485 486 $this->left . 'prev_url' . $this->right => $prev_url, 487 $this->left . 'next_url' . $this->right => $next_url, 488 $this->left . 'first_url' . $this->right => $first_url, 489 $this->left . 'last_url' . $this->right => $last_url, 490 ); 491 492 $page_theme = $this->var['page_theme'] = strtr($this->config['theme'], $theme_parse); 493 494 return $page_theme; 495 } 496} 497 498?>
PHP 分页类
Wesley13
2021-10-11
1024 0 0
点赞
收藏
评论区
加载中...