A useful PHP page-out class

Source: Internet
Author: User
  1. /*

  2. Functional PHP Page-out class
  3. Subpages
  4. */
  5. Class subpages{
  6. Private $each _disnums;//The number of entries displayed per page
  7. private $nums;//Total number of entries
  8. Private $current _page;//the currently selected page
  9. Private $sub _pages;//pages per display
  10. private $pageNums;//Total pages
  11. Private $page _array = Array ();//the array used to construct pagination
  12. Private $subPage _link;//links for each page
  13. Private $subPage _type;//shows the type of paging

  14. /*

  15. __construct is a subpages constructor that is used to run automatically when a class is created.
  16. @ $each _disnums The number of entries displayed per page
  17. @nums Total number of entries
  18. @current_num the currently selected page
  19. @sub_pages number of pages per display
  20. @subPage_link links for each page
  21. @subPage_type show the type of paging
  22. When @subpage_type=1 is the normal paging mode
  23. Example: A total of 4,523 records, each page shows 10, the current 1th/453 page [first] [prev] [next] [last page]
  24. When @subpage_type=2 is a classic page-out style
  25. Example: Current 1th 453 page [first] [prev] 1 2 3 4 5 6 7 8 9 10 [next] [last]
  26. */
  27. function __construct ($each _disnums, $nums, $current _page, $sub _pages, $subPage _link, $subPage _type) {
  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. $this->show_subpages ($subPage _type);
  39. echo $this->pagenums. " --". $this->sub_pages;
  40. }
  41. /*
  42. The __destruct destructor is called when the class is not in use, and the function is used to free resources.
  43. */
  44. function __destruct () {
  45. unset ($each _disnums);
  46. Unset ($nums);
  47. unset ($current _page);
  48. unset ($sub _pages);
  49. Unset ($pageNums);
  50. unset ($page _array);
  51. unset ($subPage _link);
  52. unset ($subPage _type);
  53. }
  54. /*
  55. The Show_subpages function is used inside the constructor. and used to determine what kind of paging to show.
  56. */
  57. function show_subpages ($subPage _type) {
  58. if ($subPage _type = = 1) {
  59. $this->subpagecss1 ();
  60. }elseif ($subPage _type = = 2) {
  61. $this->subpagecss2 ();
  62. }
  63. }
  64. /*
  65. The function used to initialize the array that created the paging.
  66. */
  67. function Initarray () {
  68. for ($i =0; $i < $this->sub_pages; $i +) {
  69. $this->page_array[$i]= $i;
  70. }
  71. return $this->page_array;
  72. }
  73. /*
  74. Construct_num_page the function used to construct the displayed entry
  75. Even if: [1][2][3][4][5][6][7][8][9][10]
  76. */
  77. function Construct_num_page () {
  78. if ($this->pagenums < $this->sub_pages) {
  79. $current _array=array ();
  80. for ($i =0; $i < $this->pagenums; $i +) {
  81. $current _array[$i]= $i +1;
  82. }
  83. }else{
  84. $current _array= $this->initarray ();
  85. if ($this->current_page <= 3) {
  86. for ($i =0; $i<>
  87. $current _array[$i]= $i +1;
  88. }
  89. }elseif ($this->current_page <= $this->pagenums && $this->current_page > $this->pagenums-$ This->sub_pages + 1) {
  90. for ($i =0; $i<>
  91. $current _array[$i]= ($this->pagenums)-($this->sub_pages) +1+ $i;
  92. }
  93. }else{
  94. for ($i =0; $i<>
  95. $current _array[$i]= $this->current_page-2+ $i;
  96. }
  97. }
  98. }
  99. return $current _array;
  100. }
  101. /*
  102. Constructing the paging of normal mode
  103. Total 4,523 records, per page 10 article, current 1th/453 page [first] [prev] [Next] [end page]
  104. */
  105. function SubPageCss1 () {
  106. $subPageCss 1str= "";
  107. $SUBPAGECSS 1str.= "Total". $this->nums. " Record, ";
  108. $subPageCss 1str.= "per page". $this->each_disnums. " Article, ";
  109. $subPageCss 1str.= "Current section". $this->current_page. " /". $this->pagenums." Page ";
  110. if ($this->current_page > 1) {
  111. $FIRSTPAGEURL = $this->subpage_link. " 1 ";
  112. $PREWPAGEURL = $this->subpage_link. ($this->current_page-1);
  113. $subPageCss 1str.= "[Home]";
  114. $subPageCss 1str.= "[prev]";
  115. }else {
  116. $subPageCss 1str.= "[Home]";
  117. $subPageCss 1str.= "[prev]";
  118. }
  119. if ($this->current_page < $this->pagenums) {
  120. $LASTPAGEURL = $this->subpage_link. $this->pagenums;
  121. $NEXTPAGEURL = $this->subpage_link. ($this->current_page+1);
  122. $subPageCss 1str.= "[next page]";
  123. $subPageCss 1str.= "[Last]";
  124. }else {
  125. $subPageCss 1str.= "[next page]";
  126. $subPageCss 1str.= "[Last]";
  127. }
  128. echo $subPageCss 1Str;
  129. }
  130. /*
  131. Constructing the pagination of Classic mode
  132. Current 1th/453 [first] [prev] 1 2 3 4 5 6 7 8 9 10 [next] [last]
  133. */
  134. function SubPageCss2 () {
  135. $subPageCss 2str= "";
  136. $subPageCss 2str.= "Current section". $this->current_page. " /". $this->pagenums." Page ";
  137. if ($this->current_page > 1) {
  138. $FIRSTPAGEURL = $this->subpage_link. " 1 ";
  139. $PREWPAGEURL = $this->subpage_link. ($this->current_page-1);
  140. $subPageCss 2str.= "[Home]";
  141. $subPageCss 2str.= "[prev]";
  142. }else {
  143. $subPageCss 2str.= "[Home]";
  144. $subPageCss 2str.= "[prev]";
  145. }
  146. $a = $this->construct_num_page ();
  147. for ($i =0; $i<>
  148. $s = $a [$i];
  149. if ($s = = $this->current_page) {
  150. $subPageCss 2str.= "[". $s. "]";
  151. }else{
  152. $url = $this->subpage_link. $s;
  153. $subPageCss 2str.= "[". $s. "]";
  154. }
  155. }
  156. if ($this->current_page < $this->pagenums) {
  157. $LASTPAGEURL = $this->subpage_link. $this->pagenums;
  158. $NEXTPAGEURL = $this->subpage_link. ($this->current_page+1);
  159. $subPageCss 2str.= "[next page]";
  160. $subPageCss 2str.= "[Last]";
  161. }else {
  162. $subPageCss 2str.= "[next page]";
  163. $subPageCss 2str.= "[Last]";
  164. }
  165. echo $subPageCss 2Str;
  166. }
  167. }
  168. ?>

Copy Code

Test page, test.php

    1. Require_once ("subpages.php");

    2. Number of bars to display per page

    3. $page _size=20;

    4. Total number of entries

    5. $nums = 1024;

    6. Number of pages per display

    7. $sub _pages=10;

    8. Get the current is the first few pages

    9. $pageCurrent =$_get["P"];

    10. if (! $pageCurrent) $pageCurrent = 1;
    11. $subPages =new Subpages ($page _size, $nums, $pageCurrent, $sub _pages, "test.php?p=", 2);
    12. ?>

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.