廢話不多說,直接看代碼吧
Pager.class.php 代碼如下
- class Pager {
- /**
- *int 總頁數
- * */
- protected $pageTotal;
- /**
- *int 上一頁
- * */
- protected $previous;
- /**
- *int 下一頁
- * */
- 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 < 1)
- $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 < 5 ? 11 : $this->currentPage + 5;
- if ($this->startPage < 1)
- $this->startPage = 1;
- if ($this->pageTotal < $this->endPage)
- $this->endPage = $this->pageTotal;
- }
- /**
- *分頁樣式
- * */
- protected function pageStyle() {
- $result = "共" . $this->pageTotal . "頁 ";
- if ($this->currentPage > 1)
- $result .= "<a href=/"" . $this->baseUri . "page=1/"><font style=/"font-family:webdings/">9</font></a> <a href=/"" . $this->baseUri . "page=$this->previous/"><font style=/"font-family:webdings/">3</font></a>";
- else
- $result .= "<font style=/"font-family:webdings/">9</font> <font style=/"font-family:webdings/">3</font>";
- for($i = $this->startPage; $i <= $this->endPage; $i ++) {
- if ($this->currentPage == $i)
- $result .= " <font color=/"#ff0000/">$i</font>";
- else
- $result .= " <a href=/"" . $this->baseUri . "page=$i/">$i</a>";
- }
- if ($this->currentPage != $this->pageTotal) {
- $result .= " <a href=/"" . $this->baseUri . "page=$this->next/"><font style=/"font-family:webdings/">4</font></a>";
- $result .= " <a href=/"" . $this->baseUri . "page=$this->pageTotal/"><font style=/"font-family:webdings/">:</font></a>";
- } else {
- $result .= " <font style=/"font-family:webdings/">4</font> <font style=/"font-family:webdings/">:</font>";
- }
- 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