废话不多说,直接看代码吧
pager.class.php 代码如下
class pager { /** *int 总页数 * */ protected $pagetotal; /** * */ protected $previous; /** * */ protected $next; /** *int 中间页起始序号 * */ protected $startpage; /** *int 中间页终止序号 * */ protected $endpage; /** *int 记录总数 * */ protected $recorbtotal; /** *int 每页显示记录数 * */ protected $pagesize; /** *int 当前显示页 * */ protected $currentpage; /** *string 基url地址 * */ protected $baseuri; /** * @return string 获取基url地址 */ public function getbaseuri() { return $this->baseuri; } /** * @return int 获取当前显示页 */ public function getcurrentpage() { return $this->currentpage; } /** * @return int 获取每页显示记录数 */ public function getpagesize() { return $this->pagesize; } /** * @return int 获取记录总数 */ public function getrecorbtotal() { return $this->recorbtotal; } /** * @param string $baseuri 设置基url地址 */ public function setbaseuri($baseuri) { $this->baseuri = $baseuri; } /** * @param int $currentpage 设置当前显示页 */ public function setcurrentpage($currentpage) { $this->currentpage=$currentpage; } /** * @param int $pagesize 设置每页显示记录数 */ public function setpagesize($pagesize) { $this->pagesize = $pagesize; } /** * @param int $recorbtotal 设置获取记录总数 */ public function setrecorbtotal($recorbtotal) { $this->recorbtotal = $recorbtotal; } /** *构造函数 * */ public function __construct() { $this->pagetotal = 0; $this->previous = 0; $this->next = 0; $this->startpage = 0; $this->endpage = 0; $this->pagesize = 20; $this->currentpage = 0; } /** *分页算法 * */ private function arithmetic() { if ($this->currentpage $this->currentpage = 1; $this->pagetotal = floor ( $this->recorbtotal / $this->pagesize ) + ($this->recorbtotal % $this->pagesize > 0 ? 1 : 0); if ($this->currentpage > 1 && $this->currentpage > $this->pagetotal) header ( 'location:' . $this->baseuri . 'page=' . $this->pagetotal ); $this->next = $this->currentpage + 1; $this->previous = $this->currentpage - 1; $this->startpage = ($this->currentpage + 5) > $this->pagetotal ? $this->pagetotal - 10 : $this->currentpage - 5; $this->endpage = $this->currentpage $this->currentpage + 5; if ($this->startpage $this->startpage = 1; if ($this->pagetotal $this->endpage) $this->endpage = $this->pagetotal; } /** *分页样式 * */ protected function pagestyle() { $result = 共 . $this->pagetotal . 页 ; if ($this->currentpage > 1) $result .= . $this->baseuri . page=1/>9 . $this->baseuri . page=$this->previous/>3; else $result .= 9 3; for($i = $this->startpage; $i $this->endpage; $i ++) { if ($this->currentpage == $i) $result .= $i; else $result .= . $this->baseuri . page=$i/>$i; } if ($this->currentpage != $this->pagetotal) { $result .= . $this->baseuri . page=$this->next/>4; $result .= . $this->baseuri . page=$this->pagetotal/>:; } else { $result .= 4 :; } return $result; } /** *执行分页 * */ public function execute() { if ($this->baseuri != && $this->recorbtotal == 0) return ; $this->arithmetic(); return $this->pagestyle (); }}调用代码(test.php 代码如下)
include_once 'pager.class.php';$pager=new pager();if (isset ( $_get ['page'] ) && ! emptyempty ( $_get ['page'] )) $pager->setcurrentpage($_get ['page']);else $pager->setcurrentpage(1); $pager->setrecorbtotal(1000);$pager->setbaseuri(test.php?);echo $pager->execute();
数据库结合 mysql 通用存储过程分页 海量数据分页 就是一个完美的分页了
我们还可继承 pager 类重写pagestyle方法就可以有不同的样式了. yes ok
