ホームページ  >  記事  >  バックエンド開発  >  PHP ページング コード (レンダリング付き)

PHP ページング コード (レンダリング付き)

WBOY
WBOYオリジナル
2016-07-25 08:52:481027ブラウズ
  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_no_array //ページ番号の配列
  12. public $start_num;クエリ文の開始値 limit
  13. private $page_change; //ページ番号が増加し始めるページ数
  14. private $URL; //現在のページの URL を取得します
  15. __construct($page_all_no, $page_num=5, $page_len=5)

  16. {
  17. $this->gt;page_all_no = intval($page_all_no);
  18. $this->page_num = intval($page_num);
  19. $this-> ;page_len = intval( $page_len);
  20. $this->URL = $_SERVER['REQUEST_URI'];
  21. $this->max_page() //最大ページ数を取得します
  22. $this->page_no (); // 現在のページ番号を取得します
  23. $this->page_no_array(); // ページ番号の配列
  24. $this->start_num(); // SQL ステートメントの制限の開始値を取得します
  25. $this ->change_page(); //増分が開始されるページ番号を取得 $this->page_change
  26. $this->getURL(); //現在の URL を取得して処理し、return
  27. }> ;
  28. プライベート関数 isArray($str ,$str_self)

  29. {
  30. if(!is_array($str)) throw new Exception("$str_self は Array 型である必要があります");
  31. }
  32. プライベート関数 page_no()

  33. {
  34. $this->page = isset($_GET['page']) $_GET['page'] : 1;
  35. if(isset($_GET['ページ']) && $_GET['ページ'] page = 1;
  36. if(isset($_GET['ページ']) && $_GET['ページ'] > $this ->page_max) $this->page = $this->page_max;
  37. return $this->page;
  38. }
  39. プライベート関数 max_page()

  40. {
  41. return $ this->page_max = $this->page_all_no page_all_no/$this->page_num);
  42. }
  43. プライベート関数change_page()

  44. {
  45. return $this-> page_change = ceil($this->page_len / 2);
  46. }
  47. プライベート関数 page_no_array()

  48. {
  49. return $this- >page_no_array = range(1, $this-> ;page_max);
  50. }
  51. プライベート関数 start_num()

  52. {
  53. return $this->start_num = $this->page_num * ($this->page - 1);
  54. }
  55. プライベート関数 getURL()

  56. {
  57. if(!empty($_SERVER['argc']) ? $_SERVER[' argc'] == 0 : strpos($_SERVER[ 'REQUEST_URI'], '?') === false)
  58. {
  59. $this->URL = $this->URL.'?';
  60. }else {
  61. $url_a = "/?page=[0 -9]{1,}/";
  62. $url_b = "/&page=[0-9]{1,}/";
  63. ereg("?page=[ 0-9]{1,}", $this ->URL) ? $this->URL = preg_replace($url_a, "?", $this->URL):
  64. ereg("&page=[0 -9]{1,}", $this->URL) ? $this->URL = preg_replace($url_b, "&", $this->URL):$this->URL = $this ->URL.'&';
  65. }
  66. return $this->URL;
  67. }
  68. private function header_info($total_data_modifier='合計数:', $total_page_modifier='合計ページ数:', $current_page_modifier='現在のページ:' , $header_info_modifier='', $header_data_color = 'red'){

  69. if($this->page_max != 1 && $this->page_all_no != 0)
  70. {
  71. $header_info = $total_data_modifier.''.$this->page_all_no.'> ;';
  72. $header_info .= $total_page_modifier.''.$this->page_max.'> ;';
  73. $header_info .= $current_page_modifier.''.$this->page.' ; ."rn";
  74. if(!empty($header_info_modifier)) $header_info = $header_info_modifier.$header_info;
  75. return $header_info;
  76. }else{
  77. return NULL;
  78. }
  79. }
  80. private 関数 first($first_format = '最初のページ ')
  81. {
  82. return $this->page == 1 ? $first_format."rn" : ''.$first_format.''."rn";
  83. }
  84. プライベート関数 last($last_format = '上一页')

  85. {
  86. if($this->page == 1) return $last_format."rn";
  87. return $this ->ページ-1 == 1 ? ''.$last_format.''."rn" : ' '.$last_format.''."rn";
  88. }
  89. プライベート関数 page_num_format($separator=' ', $left_modifier='[', $right_modifier=' ]', $both_sides=false, $current_page_color = 'red')

  90. {
  91. empty($separator) ? $separator = ' ' : $separator;
  92. $page_array = '';
  93. if($this->page_max page_len)
  94. {
  95. for ($i=0; $i page_max; $i++)
  96. {
  97. if($this->page_no_array[$i] == $this->page)
  98. {
  99. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn ";
  100. }else{
  101. if($i == 0)
  102. {
  103. $page_one = substr($this->URL,0,strlen($this->URL)-1);
  104. $the[$ i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  105. }else{
  106. $the[$i] = ''。 $left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  107. }
  108. }
  109. $page_array .= $the[$i].$separator;
  110. }
  111. if($both_sides === false)
  112. {
  113. $page_array = substr($page_array,0,strrpos($page_array, $separator));
  114. }elseif ($both_sides === true){
  115. $page_array = $ separator.$page_array;
  116. }else{
  117. throw new Exception('エラー: $both_sides はブール型である必要があります。');
  118. }
  119. }else{
  120. if($this->page {
  121. $i_start = 0;
  122. }else{
  123. $i_start = $this->page - $this->page_change;
  124. //如果最大的页码已显示,那么开始页就不会在递增
  125. if($i_start >= $this->page_max - $this->page_len){
  126. $i_start = $this->page_max - $this->page_len;
  127. }
  128. }
  129. $i_end = ($i_start+$this->page_len) - 1;
  130. for ($i = $i_start; $i {
  131. if($this->page_no_array[$i] == $this->page)
  132. {
  133. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  134. } else{
  135. if($i == 0)
  136. {
  137. $page_one = substr($this->URL,0,strlen($this->URL)-1);
  138. $the[$i] = ' '.$left_modifier.$this->page_no_array[$i].$right_modifier.''."rn";
  139. }else{
  140. $the[$i] = ''.$left_modifier.$ this->page_no_array[$i].$right_modifier.''."rn";
  141. }
  142. }
  143. $page_array .= $the[$i].$separator;
  144. }
  145. if($ Both_sides === false)
  146. {
  147. $page_array = substr($page_array,0,strrpos($page_array, $separator));
  148. }elseif ($both_sides === true){
  149. $page_array = $separator.$page_array ;
  150. }else{
  151. throw new Exception('エラー: $both_sides はブール型である必要があります。');
  152. }
  153. }
  154. return $page_array;
  155. }
  156. private function next($ next_format = '下一页')

  157. {
  158. if($this->page >= $this->page_max) return $next_format."rn";
  159. return ''.$next_format.''."rn";
  160. }< ;/p>
  161. private function end($end_format = '最終一页')

  162. {
  163. return $this->page>= $this->page_max ? $end_format."rn" : ''.$end_format.' '."rn";
  164. }
  165. public function select_page($target='self', $select_page_mode='PMA', $value_left_modifier='', $value_right_modifier= '')

  166. {
  167. if($this->page_max != 1 && $this->page_all_no != 0)
  168. {
  169. if($select_page_mode === 'NORMAL')
  170. {
  171. $page_no_array = $this ->page_no_array;
  172. }elseif($select_page_mode === 'PMA'){
  173. $page_no_array = $this->PMA_page_no_array();
  174. }else{
  175. throw new Exception('$select_page_mode は不明なモードです');
  176. }
  177. $select_page = '';
  178. $select_page .= ''。 "n";
  179. return $select_page;
  180. }else{
  181. return NULL;
  182. }
  183. }
  184. プライベート関数 PMA_page_no_array()

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

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

  220. echo '> ;';
  221. }
  222. パブリック関数 eshow()

  223. {
  224. return $this->last().$this->next();
  225. }
  226. public function showForHelp()

  227. {
  228. return $this->first('首页', '').$this->last('上一页', '').$this-> page_num_format('', '', '', false, '#FF8500').$this->next('下一页', '').$this->end('尾页', '' );
  229. }
  230. public function show()

  231. {
  232. return $this->header_info().$this->first('首页', '').$this- >last('上一页', '').$this->page_num_format('', '', '', false, '#FF8500').$this->next('下一页' , '').$this->end('尾页', '');
  233. }
  234. }
复制代码

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 tablelimit {$start_num}, {$page_num}";
  9. $page->key_change_page() // ページをめくるには矢印キー
  10. ; var_dump($sql);
  11. // 3 つのページング フォームが付属しており、必要に応じて新しいメソッドを追加できます。
  12. echo '

    '.$page->select_page('self', 'NORMAL') を呼び出します。 ''; //ここでの 2 番目のパラメータはデフォルトで PMA モードになります

  13. echo '

    '.$page->eshow()' p> ;';

  14. echo '

    '.$page->show().'

    ';
コードをコピー
3、PHP ページング クラス、レンダリング:

PHP ページング コード (レンダリング付き)


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。