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 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 pagetotal; $j++) { $newpage = $j + 1; if ($this->currentpage == $j + 1) { $link .= [ . ($j + 1) . ]; } else { $link .= request}>[ . ($j+1) . ]; } } //下一页 if ($this->currentpage pagetotal){ $nextpage = $this->currentpage + 1; $link .= request}>[>]; } // 下一列 if ($steppage total) { $nextpage = $steppage * ($this->numberoffset + 1); if ($nextpage pagetotal) { $link .= request}>[>>]; } } // 末页 if ($this->currentpage 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重定向,使得在地址栏中所有传递的信息都暴露出来了,大家可以进行完善下。