SP框架限制分页数字长度
require(SP_PATH . "/SpeedPHP.php");
require(APP_PATH . '/include/functions.php');
spRun();
functions.php
[php]
spAddViewFunction('pager', '__template_pager');
function __template_pager($params) {
if (!isset($params['pager']) || emptyempty($params['pager']))
return '';
$args = array();
foreach ($params as $k => $v)
if (!in_array($k, array('c', 'a', 'pager', 'myclass', 'mypage', 'offset')))
$args[$k] = $v;
$pagerhandle = isset($params['pager']['mypage']) ? $params['pager']['mypage'] : 'page';
$html = "
if ($params['pager']['current_page'] != $params['pager']['first_page']) {
$url = spUrl($params['c'], $params['a'], $args + array($pagerhandle => $params['pager']['prev_page']));
$html .= "";
} else {
$html .= "";
}
$offset = $params['offset'] ? $params['offset'] : 200; // 可以在内用offset=x来调整
foreach ($params['pager']['all_pages'] as $page) {
if ($page == $params['pager']['current_page']) {
$html .= "{$page}";
} else {
if (($params['pager']['current_page'] ($params['pager']['current_page'] > $params['pager']['last_page'] - $offset && $page > $params['pager']['last_page'] - $offset ) ||
( $page $params['pager']['current_page'] - $offset )
) {
$url = spUrl($params['c'], $params['a'], $args + array($pagerhandle => $page));
$html .= "{$page}";
}
}
}
if ($params['pager']['current_page'] != $params['pager']['last_page']) {
$url = spUrl($params['c'], $params['a'], $args + array($pagerhandle => $params['pager']['next_page']));
$html .= "Next >";
} else {
$html .= "Next >";
}
$html .= '
return $html;
}
模板调用方法
控制器内使用:
[php]
//资讯列表
function newslist() {
$newsobj = spClass("lib_news");
// 这里使用了spPager,同时用spArgs接受到传入的page参数
$this->results = $newsobj->spPager($this->spArgs('page', 1), 12)->findAll(null, 'id DESC');
// 这里获取分页数据并发送到smarty模板内
$this->pager = $newsobj->spPager()->getPager();
$this->display("admin/news_list.html");
}
CSS样式
[css]
.num {
clear:both; margin:0 auto; width:500px; padding:15px 0 0 0;
}
.num A {
BORDER-RIGHT: #ccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #ccc 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #ccc 1px solid; COLOR: #000; PADDING-TOP: 2px; BORDER-BOTTOM: #ccc 1px solid; TEXT-DECORATION: none
}
.num A:hover {
BORDER: #0080C0 1px solid; COLOR: #000;
}
.num A:active {
BORDER: #0080C0 1px solid; COLOR: #000;
}
.num SPAN.current {
BORDER-RIGHT: #0080C0 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #0080C0 1px solid; PADDING-LEFT: 5px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #0080C0 1px solid; COLOR: #fff; PADDING-TOP: 2px; BORDER-BOTTOM: #0080C0 1px solid; BACKGROUND-COLOR: #0080C0
}
.num SPAN.disabled {
BORDER-RIGHT: #eee 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #eee 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #eee 1px solid; COLOR: #ddd; PADDING-TOP: 2px; BORDER-BOTTOM: #eee 1px solid
}
推荐阅读