他的特点:单纯,接受参数,给出分页;灵活:可以自己决定,上、下一页的名称(比如我想叫prev、nex t诸如此类)可以决定每页展示的分页数量,跨页数提供一个包装方法,适应不同的网页结构,比如有的网页我需要每个分页包含一个li( 1
...),依次类推备注说明:提供的示范代码中有两个静态方法大家可以不必理会。这个是自己项目开发用的。Call::import,大家可以使用include代 替URL::refresh,这是地址操作类,这里暂时不做说明,以后有机会再放上来,大家可以用这个页面的方法getUrl()http://www.oschin a.net/code/snippet_182375_6242
[PHP]代码
- /**
- * 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('上一页', '1..', $this->num.'..', '下一页');
- }
- /**
- * 包装分页
- * @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);
- }
- /**
- * 获取分页
- * @param Int $num 展示分页数
- * @param Int $span 分页间隔
- */
- 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);
- }
- }
- ?>
使用示范
- $pages = new Pages(100, 20);
- $pages->warp('|');
|