搜尋
首頁後端開發php教程php通用分頁類別程式碼,仿goolge分頁樣式

  1. /**

  2. ** 通用php分頁類別。 (仿Google樣式)
  3. ** 只需提供記錄總數與每頁顯示數兩個參數。 (已附詳細使用說明..)
  4. ** 無需指定URL,連結由程式產生。方便用於檢索結果分頁。
  5. ** 表單採用GET方法提交,可確保在諸如查詢,刪除之類的操作時,不遺失URL參數。
  6. **/
  7. class Pager{
  8. //IE地址欄位址
  9. var $url;
  10. //記錄總條數
  11. var $countall;
  12. //總頁數
  13. var $page;
  14. //分頁數字連結
  15. var $thestr;
  16. //首頁、上一頁連結
  17. var $backstr;
  18. //尾頁、下一頁連結
  19. var $nextstr;
  20. //目前頁碼
  21. var $pg;
  22. //每頁顯示記錄數量
  23. var $countlist;
  24. //翻頁樣式
  25. var $style;
  26. //建構函數,實例化該類別的時候自動執行函數
  27. function Pager($countall,$countlist,$style="page"){
  28. //記錄數與每頁顯示數字不能整隊時,頁數取餘後加1
  29. $this->countall = $countall;
  30. $this->countlist = $countlist;
  31. $this->style=$style;
  32. if ($this->countall%$this->countlist! =0){
  33. $this->page=sprintf("%d",$this->countall/$this->countlist) 1;
  34. }else{
  35. $this->page=$ this->countall/$this->countlist;
  36. }
  37. $this->pg=$_GET["pg"];

  38. //保證pg在未指定的情況下為從第1頁開始
  39. if (!ereg("^[1-9][0-9]*$",$this->pg) || empty($this->pg)){
  40. $this->pg=1;
  41. }
  42. //頁碼超出最大範圍,取最大值
  43. if ($this->pg>$this->page){
  44. $ this->pg=$this->page;
  45. }
  46. //得到目前的URL。具體實作請看最底部的函式實體
  47. $this->url = Pager::getUrl();
  48. //取代錯誤格式的頁碼為正確頁碼
  49. if(isset($_GET["pg "]) && $_GET["pg"]!=$this->pg){
  50. $this->url=str_replace("?pg=".$_GET["pg"],"?pg=$ this->pg",$this->url);
  51. $this->url=str_replace("&pg=".$_GET["pg"],"&pg=$this->pg",$this- >url);
  52. }
  53. //產生12345等數字形式的分頁。
  54. if ($this->pagefor ($i=1;$ipage 1;$i ){
  55. $this->thestr =$this ->thestr.Pager::makepg($i,$this->pg);
  56. }
  57. }else{
  58. if ($this->pgfor ($i=1;$i$this - >thestr=$this->thestr.Pager::makepg($i,$this->pg);
  59. }
  60. }else{
  61. if (6 $this->pgpage){
  62. for ($i=$this->pg-4;$ ipg 6;$i ){
  63. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  64. }
  65. }else{
  66. for ($i=$this->pg-4;$i第 1 頁;$i ){
  67. $this-> thestr= $this->thestr.Pager::makepg($i,$this->pg);
  68. }
  69. }

  70. }
  71. }
  72. //產生上下頁等文字連結
  73. $this->backstr = Pager: :gotoback($this ->pg);
  74. $this->nextstr = Pager::gotonext($this->pg,$this->page);
  75. //echo ("共".$this->countall."條,每頁".$this->countlist."條,共".$this->page."頁".$這-> backstr.$this->thestr.$this->nextstr);
  76. }
  77. //產生數字分頁的輔助函數
  78. function makepg($i,$pg){
  79. if ($i==$pg){
  80. return " ".$i."";
  81. }else{
  82. return " ".$i."";
  83. }
  84. }
  85. //產生上一頁等資訊的函數
  86. function gotoback($pg){
  87. if ($pg-1>0){
  88. return $this ->gotoback=" 首頁 上一頁一個>」;
  89. }else{
  90. return $this->gotoback="首頁上一頁 ";
  91. }
  92. }
  93. //生成下一頁等資訊的函數
  94. function gotonext($pg,$page){
  95. if ($pg return " style."'>下一頁 ; 尾頁" ;
  96. }else{
  97. return " 下頁面尾頁";
  98. }
  99. } bbs.it-home.org
  100. //處理url中$pg的方法,用於自動產生pg=x
  101. 函數Replacepg ( $url,$flag,$i) {
  102. if ($flag == 1){
  103. $temp_pg = $this->pg;
  104. return str_replace("pg=".$temp_pg," pg =".($this->pg 1),$url);
  105. }else if($flag == 2) {
  106. $temp_pg = $this->pg;
  107. return str_replace(" pg =".$temp_pg,"pg=".($this->pg-1),$url);
  108. }else if($flag == 3) {
  109. $temp_pg = $this-> pg ;
  110. 回傳str_replace("pg=".$temp_pg,"pg=1",$url);
  111. }else if($flag == 4){
  112. $temp_pg = $this-> pg ;
  113. return str_replace("pg=".$temp_pg,"pg=".$this->page,$url);
  114. }else if($flag == 5){
  115. $temp_pg = $this->pg;
  116. return str_replace("pg=".$temp_pg,"pg=".$i,$url);
  117. }else{
  118. 回傳$url
  119. }
  120. }
  121. //取得目前URL的方法
  122. function getUrl(){
  123. $url="http://".$_SERVER["HTTP_HOST"];
  124. if(isset( $_SERVER ["REQUEST_URI"])){
  125. $url.=$_SERVER["REQUEST_URI"];
  126. }其他{
  127. $url.=$_SERVER["PHP_SELF"]
  128. if (!empty ($_SERVER["QUERY_STRING"])){
  129. $url.="?".$_SERVER["QUERY_STRING"];
  130. }
  131. }
  132. //在目前的URL裡加入pg=x字樣
  133. if (!ereg("(pg=|PG=|pG=|Pg=)", $url) ){
  134. if (!strpos($url,"?")){
  135. $url = $url."?pg=1";
  136. }其他{
  137. $url = $url."&pg=1";
  138. }
  139. }
  140. 回傳 $url;
  141. }
  142. }
  143. ?>
複製程式碼


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
高流量網站的PHP性能調整高流量網站的PHP性能調整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依賴注入:初學者的代碼示例PHP中的依賴注入:初學者的代碼示例May 14, 2025 am 12:08 AM

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

PHP性能:是否可以優化應用程序?PHP性能:是否可以優化應用程序?May 14, 2025 am 12:04 AM

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

PHP性能優化:最終指南PHP性能優化:最終指南May 14, 2025 am 12:02 AM

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

PHP依賴注入容器:快速啟動PHP依賴注入容器:快速啟動May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依賴注入與服務定位器PHP中的依賴注入與服務定位器May 13, 2025 am 12:10 AM

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

PHP性能優化策略。PHP性能優化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP電子郵件驗證:確保正確發送電子郵件PHP電子郵件驗證:確保正確發送電子郵件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

DVWA

DVWA

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

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器