<?php
/**
* 分页类
*/
class Pager {
var $total; // 记录总数
var $pageSize; // 每一页显示的记录数
var $currentPage; // 当前页码
var $offset; // 记录偏移量
var $pageTotal; // 总页数
var $numberOffset = 5; // 页码偏移量
var $request = ""; // 页面请求参数
//=================
//Fn: Pager
//功能:
构造函数
//=================
public function __construct ($total, $pageSize, $currentPage, $request = "") {
$this->total = $total;
$this->pageSize = $pageSize;
$this->pageOffset();
$this->pageTotal();
$this->currentPage($currentPage);
$this->request = $request;
}
//=================
//Fn: pageOffset
//功能:
数据库记录偏移量
//=================
public function pageOffset() {
return $this->offset = $this->pageSize * ($this->currentPage - 1);
}
//=================
//Fn: pageTotal
//功能:计算总页数
//=================
public function pageTotal() {
return $this->pageTotal = ceil($this->total / $this->pageSize);
}
//=================
//Fn: currentPage
//功能:设置页数
//=================
public function currentPage($currentPage) {
if (isset($currentPage)) {
$this->currentPage = intval($currentPage);
} else {
$this->currentPage = 1;
}
return $this->currentPage;
}
//=================
//Fn: nextPage
//功能:跳转到下一页
//=================
public function nextPage() {
// 显示记录数
$link = "共{$this->total}条 ";
// 页码步长
$stepPage = $this->currentPage ? ceil($this->currentPage / $this->numberOffset) : 1;
// 数字页码设定
$numberPage = ($this->pageTotal > $this->numberOffset) ? $this->numberOffset : $this->pageTotal;
// 只有一页
if ($this->total <= $this->pageSize) {
$link .= "[首页]|[末页]";
} else {
// 设置总页数和当前页
$link .= "第{$this->currentPage}/{$this->pageTotal}页 ";
// 首页
$link .= "<a title='首页' href=?pageNo=1{$this->request}>[首页]</a> ";
// 下一列
if ($stepPage > 1) {
$lastIndex = ($stepPage - 1) * $this->numberOffset;
$link .= "<a title='上一列' href=?pageNo={$lastIndex}{$this->request}>[<<]</a>";
}
// 上一页
if ($this->currentPage > 1) {
$prePage = $this->currentPage - 1;
$link .="<a title='上一页' href=?pageNo={$prePage}{$this->request}>[<]</a>";
}
// 数字页码
$i = ($stepPage - 1) * $this->numberOffset;
for ($j = $i; $j < ($i + $numberPage) && $j < $this->pageTotal; $j++) {
$newPage = $j + 1;
if ($this->currentPage == $j + 1) {
$link .= "<b>[" . ($j + 1) . "]</b>";
} else {
$link .= "<a href=?pageNo={$newPage}{$this->request}>[" . ($j+1) . "]</a>";
}
}
//下一页
if ($this->currentPage < $this->pageTotal){
$nextPage = $this->currentPage + 1;
$link .= "<a title=下一页 href=?pageNo={$nextPage}{$this->request}>[>]</a>";
}
// 下一列
if ($stepPage < $this->total) {
$nextPage = $stepPage * ($this->numberOffset + 1);
if ($nextPage < $this->pageTotal) {
$link .= "<a title=下一列 href=?pageNo={$nextpre}{$this->request}>[>>]</a>";
}
}
// 末页
if ($this->currentPage < $this->pageTotal) {
$link .= "..<a title=末页 href=?pageNo={$this->pageTotal}{$this->request}>[末页]</a>";
}
}
return $link;
}
}
?>