简单分页类
protected $pageParam = 'p'; // 分页参数名
protected $rowList; // 每页显示条数
protected $totalList;// 总条数
protected $pages; // 分页数
protected $curPage; // 当前页数
protected $warp = '';// a标签外层
protected $curClass = 'on';// 当前页css类
protected $ajaxFunc = ''; // ajax分页函数名称
protected $patterm = array(
'{msg}',
'{first}',
'{per}',
'{cur}',
'{next}',
'{last}',
);
protected $theme = "{first}{per}{cur}{next}{last}";
public function setWarp($warp)
{
$this->warp = $warp;
}
public function getWarp()
{
return $this->warp;
}
public function setCurClass($class)
{
$this->curClass = $class;
}
public function getCurClass()
{
return $this->curClass;
}
public function __construct($rowList, $totalList, $extraParam = array())
{
$this->rowList = $rowList;
$this->totalList = $totalList;
$pageParam = $this->getPageParam();
$this->curPage = isset($_REQUEST["{$pageParam}"]) ? $_REQUEST["{$pageParam}"] : 1;
$this->pages = ceil($this->totalList/$this->rowList);
if ($this->curPage < 1) {
$this->curPage = 1;
}
if ($this->curPage > $this->pages) {
$this->curPage = $this->pages;
}
$this->extraParam = $extraParam;
}
protected function format($html)
{
$warp = $this->getWarp();
if (!empty($warp) && !empty($html)) {
return "<{$warp}>{$html}</{$warp}>";
}
return $html;
}
protected function buildUrl($num)
{
$queryParmas = '';
$name = $this->getPageParam();
$params = array();
if (!empty($_SERVER['QUERY_STRING'])) {
$arr = explode('&', $_SERVER['QUERY_STRING']);
foreach ($arr as $li) {
list($k, $v) = explode('=', $li);
$params[$k] = $v;
}
}
$params[$name] = $num;
if (!empty($this->extraParam)) {
$params = array_merge($params, $this->extraParam);
}
$i = 0;
foreach ($params as $k => $v) {
$i == 0 ? ($queryParmas .= "{$k}={$v}") : ($queryParmas .= "&{$k}={$v}");$i++;
}
if (!empty($this->ajaxFunc)) {
$param = array();
if (!empty($this->extraParam)) {
$param = array_values($this->extraParam);
}
array_unshift($param, $num);
$param = implode(',', $param);
return "javascript:{$this->ajaxFunc}({$param});";
} else {
return str_replace(substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '?')), "", $_SERVER['REQUEST_URI']) . '?' . $queryParmas;
}
}
protected function buildMsg()
{
return "共{$this->pages}页, 当前第{$this->curPage}页";
}
protected function buildFirstPage()
{
$html = '';
if ($this->curPage > 1) {
$url = $this->buildUrl(1);
return "<a href='$url'>首页</a>";
}
return $html;
}
protected function buildPerPage()
{
$html = '';
if ($this->curPage != 1) {
$url = $this->buildUrl($this->curPage - 1);
return "<a href='$url'><</a>";
}
return $html;
}
protected function buildNextPage()
{
$html = '';
if ($this->curPage != $this->pages) {
$url = $this->buildUrl($this->curPage + 1);
return "<a href='$url'>></a>";
}
return $html;
}
protected function buildLastPage()
{
$html = '';
if ($this->curPage < $this->pages) {
$url = $this->buildUrl($this->pages);
return "<a href='$url'>尾页</a>";
}
return $html;
}
protected function buildCurPage()
{
$off = 3;
$count = $this->pages;
$cur = $this->curPage;
if ($count > 2*$off + 1) {
if ($cur < $off + 1) {
$first = 1;
} else {
$first = $cur - $off;
}
$last = $first + 2*$off;
if ($last > $count) {
$last = $count;
$first = $last- 2*$off;
}
} else {
$first = 1;
$last = $count;
}
$html = '';
for ($i = $first; $i <= $last; $i++) {
$url = $this->buildUrl($i);
$html .= $this->format("<a href='$url'" . (($i==$this->curPage) ? " class='{$this->getCurClass()}'" : "") . ">$i</a>");
}
return $html;
}
public function fetch($type = 1, $ajaxFunc = '')
{
if ($type == self::AJAX_PAGE) {
$this->ajaxFunc = $ajaxFunc;
}
$content = str_replace($this->patterm, array(
$this->buildMsg(),
$this->format($this->buildFirstPage()),
$this->format($this->buildPerPage()),
$this->buildCurPage(),
$this->format($this->buildNextPage()),
$this->format($this->buildLastPage()),
), $this->theme);
return $content;
}
public function getPageParam()
{
return $this->pageParam;
}
public function setPageParam($pageParam)
{
return $this->pageParam = $pageParam;
}
public function getTheme()
{
return $this->theme;
}
public function setTheme($theme)
{
return $this->theme = $theme;
}
}