废话不多说,直接看代码吧 Pager.class.php 代码如下 class Pager{ /** *int总页数 **/ protected $pageTotal ; /** **/ protected $previous ; /** **/ protected $next ; /** *int中间页起始序号 **/ protected $startPage ; /** *int中间页终止序号 **/ pr
废话不多说,直接看代码吧
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
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
1 週前ByDDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

WebStorm Mac版
好用的JavaScript開發工具

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中