Home > Backend Development > PHP Tutorial > PHP paging class and paging function, multiple paging styles to choose from

PHP paging class and paging function, multiple paging styles to choose from

WBOY
Release: 2016-07-25 08:51:37
Original
1201 people have browsed it
  1. /**

  2. * Page name: page_class.php
  3. */
  4. class Page {
  5. private $each_disNums; //Number of entries displayed per page
  6. private $nums; //Total number of entries
  7. private $current_page; //The currently selected page
  8. private $sub_pages; //Number of pages displayed each time
  9. private $pageNums; //Total number of pages
  10. private $page_array = array (); //Array used to construct paging
  11. private $ subPage_link; //Links for each page
  12. /**
  13. *
  14. * __construct is the constructor of SubPages, used to run automatically when creating a class.
  15. * @$each_disNums The number of entries displayed on each page
  16. * @nums The total number of entries
  17. * @current_num The currently selected page
  18. * @sub_pages The number of pages displayed each time
  19. * @subPage_link The link of each paging
  20. * @subPage_type Displays the type of paging
  21. *
  22. * When @subPage_type=1, it is normal paging mode
  23. * example: 4523 records in total, each The page displays 10 items, the current page 1/453 [Home] [Previous page] [Next page] [Last page]
  24. * When @subPage_type=2, it is the classic paging style
  25. * example: The current page 1/453 [ Home page] [Previous page] 1 2 3 4 5 6 7 8 9 10 [Next page] [Last page]
  26. */
  27. function Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link) {
  28. $this->each_disNums = intval($each_disNums );
  29. $this->nums = intval($nums);
  30. if (!$current_page) {
  31. $this->current_page = 1;
  32. } else {
  33. $this->current_page = intval($current_page );
  34. }
  35. $this->sub_pages = intval($sub_pages);
  36. $this->pageNums = ceil($nums / $each_disNums);
  37. $this->subPage_link = $subPage_link;
  38. }
  39. / **
  40. * Take care of low version
  41. */
  42. function __construct($each_disNums, $nums, $current_page, $sub_pages, $subPage_linke) {
  43. $this->Page($each_disNums, $nums, $current_page, $sub_pages, $subPage_link) ;
  44. }

  45. /*

  46. __destruct destructor, which is called when the class is no longer in use. This function is used to release resources.
  47. */
  48. function __destruct() {
  49. unset ($each_disNums);
  50. unset ($nums);
  51. unset ($current_page);
  52. unset ($sub_pages);
  53. unset ($pageNums);
  54. unset ($page_array) ;
  55. unset ($subPage_link);
  56. }

  57. /*

  58. Function used to initialize the paging array.
  59. */
  60. function initArray() {
  61. for ($i = 0; $i < $this->sub_pages; $i++) {
  62. $this->page_array[$i] = $i;
  63. }
  64. return $this->page_array;
  65. }

  66. /*

  67. construct_num_Page该函数使用来构造显示的条目
  68. 即使:[1][2][3][4][5][6][7][8][9][10]
  69. */
  70. function construct_num_Page() {
  71. if ($this->pageNums < $this->sub_pages) {
  72. $current_array = array ();
  73. for ($i = 0; $i < $this->pageNums; $i++) {
  74. $current_array[$i] = $i +1;
  75. }
  76. } else {
  77. $current_array = $this->initArray();
  78. if ($this->current_page <= 3) {
  79. for ($i = 0; $i < count($current_array); $i++) {
  80. $current_array[$i] = $i +1;
  81. }
  82. }
  83. elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {
  84. for ($i = 0; $i < count($current_array); $i++) {
  85. $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;
  86. }
  87. } else {
  88. for ($i = 0; $i < count($current_array); $i++) {
  89. $current_array[$i] = $this->current_page - 2 + $i;
  90. }
  91. }
  92. }

  93. return $current_array;

  94. }

  95. /*

  96. 构造普通模式的分页
  97. 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
  98. */
  99. function subPageCss1() {
  100. $subPageCss1Str = "";
  101. $subPageCss1Str .= "共" . $this->nums . "条记录,";
  102. $subPageCss1Str .= "每页显示" . $this->each_disNums . "条,";
  103. $subPageCss1Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";
  104. if ($this->current_page > 1) {
  105. $firstPageUrl = $this->subPage_link . "1";
  106. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  107. $subPageCss1Str .= "[首页] ";
  108. $subPageCss1Str .= "[上一页] ";
  109. } else {
  110. $subPageCss1Str .= "[首页] ";
  111. $subPageCss1Str .= "[上一页] ";
  112. }

  113. if ($this->current_page < $this->pageNums) {

  114. $lastPageUrl = $this->subPage_link . $this->pageNums;
  115. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  116. $subPageCss1Str .= " [下一页] ";
  117. $subPageCss1Str .= "[尾页] ";
  118. } else {
  119. $subPageCss1Str .= "[下一页] ";
  120. $subPageCss1Str .= "[尾页] ";
  121. }

  122. return $subPageCss1Str;

  123. }

  124. /*

  125. 构造经典模式的分页
  126. 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
  127. */
  128. function subPageCss2() {
  129. $subPageCss2Str = "";
  130. $subPageCss2Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";

  131. if ($this->current_page > 1) {

  132. $firstPageUrl = $this->subPage_link . "1";
  133. $prewPageUrl = $this->subPage_link . ($this->current_page - 1);
  134. $subPageCss2Str .= "[首页] ";
  135. $subPageCss2Str .= "[上一页] ";
  136. } else {
  137. $subPageCss2Str .= "[首页] ";
  138. $subPageCss2Str .= "[上一页] ";
  139. }

  140. $a = $this->construct_num_Page();

  141. for ($i = 0; $i < count($a); $i++) {
  142. $s = $a[$i];
  143. if ($s == $this->current_page) {
  144. $subPageCss2Str .= "[" . $s . "]";
  145. } else {
  146. $url = $this->subPage_link . $s;
  147. $subPageCss2Str .= "[" . $s . "]";
  148. }
  149. }

  150. if ($this->current_page < $this->pageNums) {

  151. $lastPageUrl = $this->subPage_link . $this->pageNums;
  152. $nextPageUrl = $this->subPage_link . ($this->current_page + 1);
  153. $subPageCss2Str .= " [下一页] ";
  154. $subPageCss2Str .= "[尾页] ";
  155. } else {
  156. $subPageCss2Str .= "[下一页] ";
  157. $subPageCss2Str .= "[尾页] ";
  158. }
  159. return $subPageCss2Str;
  160. }
  161. }

  162. //Test two different effects

  163. $t = new Page(10, 100, $_GET['p'], 5, 'page_class. php?p=');
  164. echo $t->subPageCss2(); //Call the classic paging function

  165. echo "
    ";

  166. echo $t->subPageCss1 ();//Call the normal paging function

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template