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

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

WBOY
WBOY原創
2016-07-25 08:52:481081瀏覽
  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