- /**
- * 分页类
- */
- 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 .= "request}>[首页] ";
-
- // 下一列
- if ($stepPage > 1) {
- $lastIndex = ($stepPage - 1) * $this->numberOffset;
- $link .= "request}>[<<]";
- }
-
- // 上一页
- if ($this->currentPage > 1) {
- $prePage = $this->currentPage - 1;
- $link .="request}>[<]";
- }
-
- // 数字页码
- $i = ($stepPage - 1) * $this->numberOffset;
-
- for ($j = $i; $j < ($i + $numberPage) && $j < $this->pageTotal; $j++) {
- $newPage = $j + 1;
- if ($this->currentPage == $j + 1) {
- $link .= "[" . ($j + 1) . "]";
- } else {
- $link .= "request}>[" . ($j+1) . "]";
- }
- }
- //下一页
- if ($this->currentPage < $this->pageTotal){
- $nextPage = $this->currentPage + 1;
- $link .= "request}>[>]";
- }
-
- // 下一列
- if ($stepPage < $this->total) {
- $nextPage = $stepPage * ($this->numberOffset + 1);
- if ($nextPage < $this->pageTotal) {
- $link .= "request}>[>>]";
- }
- }
- // 末页
- if ($this->currentPage < $this->pageTotal) {
- $link .= "..pageTotal}{$this->request}>[末页]";
- }
-
- }
- return $link;
- }
- }
- ?>
二,php分页类调用示例:
1,获取URL传回来的page页数:
- $cur_page = 1;
- if (isset($_GET["pageNo"])) {
- $cur_page = $_GET["pageNo"];
- }
2,创建分页对象:
- $nums:某数据的总数
- $page_size:每页显示数
- $cur_page:当前页数
- $request:其他Url请求可选参数
- $pager = new Pager($nums, $page_size, $cur_page, $request);
3,smarty赋值:
- $show = 得到要显示的数据
- $this->tpl->assign('numlink', $pager->nextPage()); // 得到分页列表
- $this->tpl->assign('data',$show);
分页效果:

以上分页代码没有实现url重定向,使得在地址栏中所有传递的信息都暴露出来了,大家可以进行完善下。 |