<?php
/**
* 后台通用分页类
*/
class Pager
{
//public
public $firstPage;
public $endPage;
public $lastPage;
public $nextPage;
public $thisPage;
public $pageSize;
public $rowNum;
public $listNum;
public $cssClass;
public $splitStr;
public $pageLast;
public $pageNext;
public $pageFirst;
public $pageEnd;
public $pageList;
public $pageSelect;
public $pageAll;
public $links;
function __construct($rowNum,$pageSize = '20',$listNum = '8')
{
$this->cssClass = 'current';
$this->rowNum = $rowNum;
$this->pageSize = $pageSize;
$this->listNum = $listNum;
$this->splitStr = ' ';
$this->lastPage = '上一页';
$this->nextPage = '下一页';
$this->firstPage = '首页';
$this->endPage = '末页';
$this->thisPage = $_GET['page'] > 0 ? $_GET['page'] : 1;
}
function getStrWhere()
{
$temp = array();
foreach ($_GET as $key=>$val)
if($key != 'page') $temp[] = urlencode($key) . '=' . urlencode($val);
foreach ($_POST as $key=>$val)
$temp[] = urlencode($key) . '=' . urlencode($val);
$str = implode('&',$temp);
unset($temp);
return $str;
}
function getPageNum()
{
if ($this->thisPage < 1)
$this->thisPage = 1;
if ($this->rowNum < 1)
return 1;
return ceil($this->rowNum/$this->pageSize);
}
function createSelect($s,$e)
{
$temp = array();
$temp[] = '<select class="'.$this->cssClass.'" onchange="window.location=this.options[this.selectedIndex].value">';
for($i = $s ; $i <= $e;$i ++)
$temp[] = '<option value="?page='.$i.''.$this->getStrWhere().'" '.(($i == $this->thisPage)?'selected':'').'>'.$i;
$temp[] = '<select>';
return implode(chr(13),$temp);
}
function createPage()
{
$strWhere = $this->getStrWhere();
$lastPage = $this->lastPage;
$firstPage = $this->firstPage;
$endPage = $this->endPage;
$nextPage = $this->nextPage;
$thisPage = $this->thisPage;
$pageNum = $this->getPageNum();
$listNum = $this->listNum;
$startNum = $thisPage - (ceil($listNum)-1);
if ($startNum < 1 ) $startNum = 1;
$endNum = $startNum + $listNum - 1;
if ($endNum > $pageNum) $endNum = $pageNum;
$this->links['first'] ='?'.$strWhere.'&page=1';
$this->links['last'] ='?'.$strWhere.'&page='.(($thisPage-1<1)?1:($thisPage-1));
$this->links['next'] ='?'.$strWhere.'&page='.(($thisPage+1>$pageNum)?$pageNum:($thisPage+1));
$this->links['end'] ='?'.$strWhere.'&page='.$pageNum;
$this->pageFirst = '<a href="'.$this->links['first'].'" class="'.$this->cssClass.'">'.$firstPage.'</a>'.$this->splitStr;
$this->pageLast = '<a href="'.$this->links['last'].'" class="'.$this->cssClass.'">'.$lastPage.'</a>';
$this->pageNext = '<a href="'.$this->links['next'].'" class="'.$this->cssClass.'">'.$nextPage.'</a>';
$this->pageEnd = $this->splitStr.'<a href="'.$this->links['end'].'" class="'.$this->cssClass.'">'.$endPage.'</a>';
$pageList;
for($i = $startNum ; $i <= $endNum ; $i ++)
if ($thisPage != $i)
$pageList[] = '<a href="?'.$strWhere.'&page='.$i.'" class="'.$this->cssClass.'">'.$i.'</a>';
else
$pageList[] = '<a class="'.$this->cssClass.'"><b>'.$i.'</b></a>';
$this->pageList = implode($this->splitStr,$pageList);
$this->pageSelect = $this->createSelect($startNum,$endNum);
//带跳转
//$this->pageAll = $this->pageFirst.$this->splitStr.$this->pageLast.$this->splitStr.$this->pageList.$this->splitStr.$this->pageNext.$this->splitStr.$this->pageEnd.$this->splitStr.$this->pageSelect;
//不带跳转
$this->pageAll = $this->pageFirst.$this->splitStr.$this->pageLast.$this->splitStr.$this->pageList.$this->splitStr.$this->pageNext.$this->splitStr.$this->pageEnd.$this->splitStr;
return $this->pageAll;
}
}
?>
#demo
$total = 100;
$pageSize = 20;
$obj = new Pager($total,$pageSize);
$obj->createPage();