php通用分頁類別程式碼,仿goolge分頁樣式

WBOY
發布: 2016-07-25 08:52:37
原創
1076 人瀏覽過
  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. ?>
複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!