Its characteristics: simple, accepts parameters and gives pagination; flexible: you can decide by yourself. The names of the previous and next pages (for example, I want to call them prev, next, etc.) can determine the number of paginations displayed on each page and the number of cross-pages. Provide a packaging method to adapt to different web page structures. For example, for some web pages, I need each page to contain a li(
1
...), and so on. Note: There are two static methods in the provided sample code, which you can ignore. This is for my own project development. Call::import, you can use include instead of URL::refresh. This is an address operation class. I will not explain it here for now. I will put it up when I have the opportunity. You can use the method getUrl() on this page http://www. oschin a.net/code/snippet_182375_6242
[PHP] code
- /**
- * FILE_NAME : Pages.php FILE_PATH : /lv/view/
- * 分页类
- *
- * @copyright Copyright (c) 2006-2010 mailTo:levi@cgfeel.com
- * @author Levi
- * @package lv.view.Pages
- * @subpackage
- * @version 2011-10-03
- */
- Call::import('lv.url.URL');
- class Pages
- {
- public $num = 1;
- public $size = 50;
- public $current = 1;
- private $_pages = array();
- private $_title = array();
- public function __construct($count, $size = 50)
- {
- $this-> num = ceil($count / $size);
- $size > 0 && $this->size = (Int)$size;
-
- $page = isset($_GET['page']) ? (Int) trim($_GET['page']) : 1;
- $page > 1 && $this->current = (Int)$page;
-
- $this->_title = array('previous page', '1..', $this->num.'..', 'Next page');
- }
-
- /**
- * Package paging
- * @param String|Array $skirt
- * @param Array $entitle
- * @return String
- */
- public function warp($skirt, $entitle = array( ))
- {
- empty($this->_pages) && $this->get();
-
- $entitle += $this->_title;
- $skirt = (Array)$skirt + array(' ', NULL);
- $data = implode($skirt[1].$skirt[0], $this->_pages);
- !is_null($skirt[1]) && $data = $skirt[0] .$data.$skirt[1];
-
- return vsprintf($data, $entitle);
- }
-
- /**
- * Get pagination
- * @param Int $num display pagination number
- * @param Int $span pagination interval
- */
- public function get($num = '5', $span = ' 2')
- {
- $this->_pages = array();
- $start = $this->current - $num + $span;
- $start < 1 && $start = 1;
- $end = $start + $num;
- $end > $this->num && $end = (Int)$this->num;
-
- $this->current > 1 && $this->_pages [] = $this->_setPage($this->current - 1, '%1$s');
- $start > 1 && $this->_pages[] = $this->_setPage( NULL, '%2$s');
- for($i = $start; $i <= $end; $i++) $this->_pages[] = $this->_setPage($i, $ i);
-
- $end < $this->num && $this->_pages[] = $this->_setPage($this->num, '%3$s');
- $this ->current < $this->num && $this->_pages[] = $this->_setPage($this->current + 1, '%4$s');
-
- return ( Array)$this->_pages;
- }
-
- private function _setPage($page, $name = '%s')
- {
- $hover = $page == $this->current ? ' class="pageHover "' : '';
- return sprintf('%s', URL::refresh(array('page' => $page)), $ hover, $name);
- }
- }
- ?>
Copy code
Usage Demonstration
- $pages = new Pages(100, 20);
- $pages->warp('|');
Copy code
|