首頁 後端開發 php教程 一個php分頁類別代碼(附效果圖)

一個php分頁類別代碼(附效果圖)

Jul 25, 2016 am 08:52 AM

  1. /**

  2. * 簡單分頁類別
  3. * Page.class.php
  4. */
  5. class Page
  6. {
  7. private $page_num; //每頁顯示的資訊條數
  8. private $page_all_no; //訊息的總條數
  9. private $page_len; //顯示幾個頁碼
  10. private $page; //目前的頁數
  11. private $page_maxprivate $page_max ; //頁數最大值
  12. private $page_no_array; //頁數的陣列
  13. public $start_num; //查詢語句limit的起始值
  14. private $page_change; //在第幾個頁碼開始頁碼遞增
  15. private $URL; //取得目前頁面的URL
  16. public function __construct($page_all_no, $page_num=5, $page_len=5)

  17. {
  18. $this->page_all_no = intval($page_all_no);
  19. $this->page_num = intval($page_num);
  20. $this->page_len = intval($page_len);
  21. $this-> URL = $_SERVER['REQUEST_URI'];
  22. $this->max_page(); //得到頁數的最大值
  23. $this->page_no(); //得到目前頁數
  24. $ this->page_no_array(); //頁數數組
  25. $this->start_num(); //得到sql語句中limit的起始值
  26. $this->change_page(); //得到遞增開始的頁碼$this->page_change
  27. $this->getURL(); //得到目前的URL並處理回傳
  28. }
  29. private function isArray($str,$str_self )

  30. {
  31. if(!is_array($str)) throw new Exception("$str_self must be an Array type");
  32. }
  33. private function page_no( )

  34. {
  35. $this->page = isset($_GET['page']) ? $_GET['page'] : 1;
  36. if(isset($_GET['page']) && $_GET['page'] page = 1;
  37. if(isset($_GET['page']) && $_GET['page'] > $this->page_max) $ this->page = $this->page_max;
  38. return $this->page;
  39. }
  40. private function max_page()

  41. {
  42. return $this ->page_max = $this->page_all_no page_all_no/$this->page_all_no page_all_no/$this->page_num);
  43. }
  44. private function change_page()

  45. {
  46. return $this->page_change = ceil($this->page_len / 2);
  47. }
  48. private function page_no_array()

  49. {
  50. {
  51. return $this->page_no_array = range(1, $this->page_max);
  52. }
  53. private function start_num()

  54. {
  55. return $this->start_num = $this->page_num * ($this->page - 1);
  56. }
  57. private function getURL()

  58. {
  59. if(!empty($_SERVER ['argc']) ? $_SERVER['argc'] == 0 : strpos($_SERVER['REQUEST_URI'], '?') === false)
  60. {
  61. $this->URL = $this->URL.'?';
  62. }else{
  63. $url_a = "/?page=[0-9]{1,}/";
  64. $url_b = "/&page=[ 0-9]{1,}/";
  65. ereg("?page=[0-9]{1,}", $this->URL) ? $this->URL = preg_replace($url_a, " ?", $this->URL):
  66. ereg("&page=[0-9]{1,}", $this->URL) ? $this->URL = preg_replace($url_b, "&" , $this->URL):$this->URL = $this->URL.'&';
  67. }
  68. return $this->URL;
  69. }
  70. private function header_info($total_data_modifier='總數:', $total_page_modifier=' 總頁數:', $current_page_modifier=' 目前頁:', $header_info_modifier='', $header_data_color = 'redo'){
  71. if($this->page_max != 1 && $this->page_all_no != 0)
  72. {
  73. $header_info = $total_data_modifier.' '.$this->page_all_no.'';
  74. $header_info .= $total_page_modifier.''.$this ->page_max.'';
  75. $header_info .= $current_page_modifier.''.$this->page. ' '."rn";
  76. if(!empty($header_info_modifier)) $header_info = $header_info_modifier.$header_info;
  77. return $header_info;
  78. }else{
  79. return NULL;
  80. }
  81. }
  82. private function first($first_format = '第一頁')

  83. {
  84. return $this->page = = 1 ? $first_format."rn" : ''. $first_format.''."rn";}
  85. 蕨函數last($last_format = '上一頁')

  86. {
  87. if($this->page == 1) return $last_format."rn ";
  88. 回傳$this->page-1 == 1 ? ''.$last_format.''. "rn" : ' '.$last_format.''."rn";
  89. }
  90. 真空函式page_num_format($separator=' ', $left_modifier='[', $right_modifier=']'、$both_sides=false、 $current_page_color = '紅色')

  91. {
  92. 空($separator) ? $separator = ' ' : $separator;
  93. $page_array = '';
  94. if($this->page_max page_len)
  95. {
  96. for ($i =0; $i $i )
  97. {
  98. if($this->page_no_array[$i] == $this->page)
  99. {
  100. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn ";
  101. }else{
  102. if($i == 0)
  103. {
  104. $page_one = substr($this->URL,0,strlen($this->URL)- 1);
  105. $ the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  106. }else{
  107. $the[$i] = ''。 $left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  108. }
  109. }
  110. $page_array .= $the[$i].$分隔符號;
  111. }
  112. if($both_sides === false)
  113. {
  114. $page_array = substr($page_array,0 ,strrpos($page_array, $separator));
  115. }
  116. }
  117. } elseif ($both_sides === true){
  118. $page_array = $separator.$page_array;
  119. }else{
  120. 引發新的異常( '錯誤:$both_sides 必須是布林類型。');
  121. }
  122. }else{
  123. if($this->page page_change)
  124. {
  125. $i_start = 0;
  126. }else{
  127. $i_start = $this->page - $this->page_change;
  128. //如果最大的頁碼已顯示,那麼開始頁不會多了會遞增
  129. if($i_start >= $this->page_max - $ this->page_len){
  130. $i_start = $this->page_max - $this->page_len;
  131. }
  132. }
  133. $i_end = ($i_start $this->page_len) - 1;
  134. 對於($i = $i_start; $i {
  135. if($this->page_no_array[$i] == $this->page)
  136. {
  137. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.' '."rn";
  138. }else{
  139. if($i == 0)
  140. {
  141. $page_one = substr($this->URL,0,strlen($this-> ;URL)-1);
  142. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$ right_modifier.''."rn" ;
  143. }else{
  144. $the[$i] = '' .$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  145. }
  146. }
  147. $page_array .= $the[$i]. $separator;
  148. }
  149. if($both_sides === false)
  150. {
  151. $page_array = substr( $page_array,0,strrpos($page_array, $separator));
  152. } elseif ($both_sides === true){
  153. $page_array = $separator.$page_array;
  154. }else{
  155. throw new Exception('錯誤:$both_sides必須是布林類型。');
  156. }
  157. }
  158. return $page_array;
  159. }
  160. 私有函數next($next_format = '下一頁')

  161. {
  162. if($ this->page >= $this->page_max) return $next_format."rn";
  163. return ' '.$next_format.' '."rn";
  164. }
  165. private function end($end_format = '最後頁')

  166. {
  167. return $this->page > ;= $this->page_max ? $end_format."rn" : ''.$end_format.' '."rn";
  168. }
  169. 公用函式 select_page($target='self', $select_page_mode='PMA', $value_left_modifier='' , $value_right_modifier ='')

  170. {
  171. if($this->page_max != 1 && $this->page_all_no != 0)
  172. {
  173. if($select_page_mode === '正常')
  174. {
  175. $page_no_array = $this->page_no_array;
  176. }elseif($select_page_mode === 'PMA'){
  177. $page_no_array = $this->PMA_page_nono_array( ); {
  178. 引發新的例外('$select_page_mode 是未知模式');}
  179. $select_page = '';
  180. $select_page .= ''。 return $select_page;
  181. }else{
  182. return NULL;
  183. }
  184. }
  185. 原生函數PMA_page_no_array()

  186. {
  187. $showAll = 200 ;
  188. $sliceStart = 5;
  189. $sliceEnd = 5;
  190. $percent = 20;
  191. $range = 10;
  192. if ($this->page_max $this->PMA_page_no_array = range(1, $this->page_max);

  193. } else {
  194. $ this->PMA_page_no_array = array();
  195. for ($ i = 1; $i page_max; $i ) {
  196. $this->PMA_page_no_array[] = $i;
  197. }
  198. $i = $sliceStart;
  199. $x = $this->page_max - $sliceEnd;
  200. $met_boundary = false;
  201. while ($i if ($i >= ($this->page - $range) && $i page $range)) {
  202. $i ;
  203. $ met_boundary = true;
  204. } else {
  205. $i = $i 樓層($this->page_max / $percent);
  206. if ($i > ($this->page - $range) ) && !$met_boundary) {
  207. $i = $this->page - $range;
  208. }
  209. }
  210. if ($i > 0 && $i $this->PMA_page_no_array[] = $i;
  211. }
  212. }
  213. sort($this->PMA_page_no_array);
  214. $this->PMA_page_no_array = array_unique($this->PMA_page_no_array);}PMA_page_no_array;
  215. }
  216. public function key_change_page(){

  217. echo '< ;script type="text/javascript">
  218. document.onkeydown=Page_keypress ;
  219. function Page_keypress(e){
  220. if(!e)var e = window.event;
  221. var kc = e.which||e.keyCode;
  222. switch(kc){
  223. 案例37:
  224. if(document.getElementById("kl_last") != null) location.href=document.getElementById("kl_last ").href;
  225. 中斷;
  226. 案例39:
  227. if(document.getElementById("kl_next") != null) location.href=document.getElementById("kl_next").href;
  228. 中斷;
  229. }
  230. }
  231. ' ;
  232. }
  233. public function eshow()

  234. {
  235. return $this->last().$this->next();
  236. } p>
  237. public function showForHelp()

  238. {
  239. return $this->first('首頁', '').$this->last('上一頁', '') .$this->page_num_format('', '', '', false, '#FF8500 ').$this->next('下一頁', '').$this->end('尾頁' , '');
  240. }
  241. public function show()
  242. {
  243. return $this->header_info().$this->first('首頁', '').$this->last('上一頁', '').$this->page_num_format('', '', '', false, '#FF8500').$this->next('下一頁', '').$this- >end('尾頁', '');
  244. }
  245. }
  246. 複製程式碼
  247. 2,php分頁類別的使用方法:

    1. header('content-type:text/html;charset=utf-8');
    2. include('Page.class.php') ;
    3. $page_all_no = 12000; //資料的總條數
    4. $page_num = 25; //設定每頁顯示的條數
    5. $page_len = 7; //顯示最多的頁碼數
    6. $page = new Page($page_all_no, $page_num, $page_len);
    7. $start_num = $page->start_num;
    8. $sql = "select * from table limit {$start_num}, {$page_num} ";
    9. $page->key_change_page(); //方向鍵翻頁
    10. var_dump($sql);
    11. //自帶三種分頁形式,可再按需要新增方法,像下面那樣呼叫
    12. echo '

      '.$page->select_page('self', 'NORMAL').'

      '; //這裡的第二個參數預設為PMA模式,兩種模式差異在上面已給出
    13. echo '

      '.$page->eshow().'

      ';
    14. echo '

      '.$page->show().'

      ';
    複製程式碼

    3,php分頁類,效果圖: 一個php分頁類別代碼(附效果圖)



本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

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

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

php中的捲曲:如何在REST API中使用PHP捲曲擴展 php中的捲曲:如何在REST API中使用PHP捲曲擴展 Mar 14, 2025 am 11:42 AM

PHP客戶端URL(curl)擴展是開發人員的強大工具,可以與遠程服務器和REST API無縫交互。通過利用Libcurl(備受尊敬的多協議文件傳輸庫),PHP curl促進了有效的執行

在Codecanyon上的12個最佳PHP聊天腳本 在Codecanyon上的12個最佳PHP聊天腳本 Mar 13, 2025 pm 12:08 PM

您是否想為客戶最緊迫的問題提供實時的即時解決方案? 實時聊天使您可以與客戶進行實時對話,並立即解決他們的問題。它允許您為您的自定義提供更快的服務

解釋PHP中晚期靜態結合的概念。 解釋PHP中晚期靜態結合的概念。 Mar 21, 2025 pm 01:33 PM

文章討論了PHP 5.3中介紹的PHP中的晚期靜態結合(LSB),允許靜態方法的運行時間分辨率調用以更靈活的繼承。 LSB的實用應用和潛在的觸摸

在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

框架安全功能:防止漏洞。 框架安全功能:防止漏洞。 Mar 28, 2025 pm 05:11 PM

文章討論了框架中的基本安全功能,以防止漏洞,包括輸入驗證,身份驗證和常規更新。

自定義/擴展框架:如何添加自定義功能。 自定義/擴展框架:如何添加自定義功能。 Mar 28, 2025 pm 05:12 PM

本文討論了將自定義功能添加到框架上,專注於理解體系結構,識別擴展點以及集成和調試的最佳實踐。

如何用PHP的cURL庫發送包含JSON數據的POST請求? 如何用PHP的cURL庫發送包含JSON數據的POST請求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL庫發送JSON數據在PHP開發中,經常需要與外部API進行交互,其中一種常見的方式是使用cURL庫發送POST�...

See all articles