Php paging class with multiple paging methods

Source: Internet
Author: User
Php paging class with multiple paging methods

  1. Class PAGE {

  2. // Starts the class
  3. /*************************************** *****************
  4. * $ Total number of records
  5. * $ PageNum number of entries per page
  6. * $ Url = ''link
  7. * $ Page-> StartPage (display classification statistics, character classification/numeric paging, jump );
  8. * $ Page-> StartPage (true/false, true/false, true/false );
  9. * Site http://bbs.it-home.org
  10. **************************************** *****************/
  11. Private $ total; // The total number of records.
  12. Private $ pageNum; // Number of Entries displayed per page
  13. Private $ page; // The current page number.
  14. Private $ pages; // The total number of pages.
  15. Private $ url; // page url
  16. Private $ Aque; // URL parameter
  17. /* Constructor */
  18. Public function PAGE ($ total, $ pageNum, $ url = '? '){
  19. $ This-> total = $ total; // The total number of records.
  20. $ This-> pageNum = $ pageNum; // Number of Entries displayed per page.
  21. $ This-> url = $ this-> StrSift ($ url); // checks whether the value of $ url is valid.
  22. $ This-> Aque =$ _ GET; // parameters originally passed on the page.
  23. $ This-> page = $ this-> StrSift ($ _ GET ['Page']); // GET (global variable) mode parameter of the current page, current page number.
  24. $ This-> page = is_numeric ($ this-> page )? $ This-> page: 1; // when the current page number is not a number, set it to 1.
  25. $ This-> pages = ceil ($ total/$ pageNum); // The total number of pages.
  26. If ($ this-> page <1) $ this-> page = 1; // when the page number is smaller than 1, set it to 1.
  27. // If ($ this-> page> $ this-> pages) $ this-> page = $ this-> pages; // when the page number is greater than the maximum page number, set it to the maximum page number.
  28. }
  29. /******************
  30. * Paging method
  31. *******************/
  32. Function StartPage ($ str, $ view = true, $ jump = true ){
  33. If ($ view = true) $ PageStr. = $ this-> GetCount (); // page statistics
  34. If ($ str = 'str') $ PageStr. = $ this-> GetPageStr (); // select the character paging form
  35. Else $ PageStr. = $ this-> GetPageNum (); // select the number pagination form
  36. If ($ jump = true) $ PageStr. = $ this-> JumpSelect (); // jump
  37. Return $ PageStr;
  38. }

  39. /*************************************** *******

  40. * Display statistics. Format: 5 Record pages: 2/3
  41. **************************************** *******/
  42. Function GetCount (){
  43. $ CountStr = "total". $ this-> total. "record page:". $ this-> page. "/". $ this-> pages ."";
  44. Return $ CountStr;
  45. }

  46. /*************************************** ********

  47. * Page format 1: First Page Previous Page Last Page
  48. **************************************** ********/
  49. Function GetPageStr (){
  50. $ Url = $ this-> url; // Obtain the URL
  51. // Process URL parameters: the key of the array is the URL variable, and the value of the array is the value of the URL variable.
  52. Foreach ($ this-> Aque as $ key => $ val ){
  53. Switch ($ key ){
  54. Case "page ":
  55. $ Next = $ val + 1;
  56. $ Prev = $ val-1;
  57. Break;
  58. Default:
  59. $ Sque. = "& $ key =". $ this-> StrSift ($ val );
  60. }
  61. }
  62. If ($ Next = 0) $ Next = 2;
  63. // Previous Page
  64. Switch ($ this-> page ){
  65. Case $ this-> page <= 1:
  66. $ Pagestr. = "first page ";
  67. $ Pagestr. = "previous page ";
  68. Break;
  69. Default:
  70. $ Pagestr. = "first page ";
  71. $ Pagestr. = "previous page ";
  72. }
  73. // The last page of The Next page
  74. Switch ($ this-> page ){
  75. Case $ this-> page >=$ this-> pages:
  76. $ Pagestr. = "next page ";
  77. $ Pagestr. = "Last page ";
  78. Break;
  79. Default:
  80. $ Pagestr. = "next page ";
  81. $ Pagestr. = "pages $ Sque '> last page ";
  82. }
  83. // Returns the paging string.
  84. Return $ pagestr;
  85. }

  86. /*************************************** ********************

  87. * Page format: 4307 records page: 1/72 1 2 3 4 5 6 7 8 9 10
  88. **************************************** ********************/
  89. Function GetPageNum (){
  90. $ Url = $ this-> url;
  91. // Process URL parameters: the key of the array is the URL variable, and the value of the array is the value of the URL variable.
  92. Foreach ($ this-> Aque as $ key => $ val ){
  93. Switch ($ key ){
  94. Case $ key! = "Page ":
  95. $ Sque. = "& $ key =". $ this-> StrSift ($ val );
  96. }
  97. }
  98. Switch ($ this-> pages ){
  99. // The total number of pages is greater than 12:
  100. Case $ this-> pages> 12:
  101. // Before the number displayed on the page: <
  102. Switch ($ this-> page ){
  103. Case $ this-> page> 1:
  104. $ Pagestr. = "<";
  105. $ Pagestr. = "page-1). $ Sque." '> <";
  106. Break;
  107. Default:
  108. $ Pagestr. = "<";
  109. $ Pagestr. = "<";
  110. }
  111. // Pagination number: 1 2 3 4 5 6 the current page number has 6 pagination links on the left and 6 pagination links on the right.
  112. For ($ I = $ this-> page-6; $ I <= $ this-> page + 6; $ I ++ ){
  113. If ($ I> $ this-> pages) break;
  114. If ($ I ==$ this-> page) $ pagestr. = $ I ."";
  115. Elseif ($ I> = 1) $ pagestr. = "$ I ";
  116. }
  117. // After the paging number: >>>
  118. Switch ($ this-> page ){
  119. Case $ this-> page <$ this-> pages:
  120. $ Pagestr. = "page + 1). $ Sque." '>>> ";
  121. $ Pagestr. = "pages. $ Sque." '> ";
  122. Break;
  123. Default:
  124. $ Pagestr. = "> ";
  125. $ Pagestr. = "> ";
  126. }
  127. Break;
  128. Default:
  129. // The total number of pages is less than 12:
  130. For ($ I = 1; $ I <= $ this-> pages; $ I ++ ){
  131. Switch ($ I ){
  132. Case $ I ==$ this-> page:
  133. $ Pagestr. = $ I ."";
  134. Break;
  135. Default:
  136. $ Pagestr. = "$ I ";
  137. }
  138. }
  139. }
  140. // Returns the paging string.
  141. Return $ pagestr;
  142. }
  143. /************************
  144. * Define the jump page. BEGIN
  145. *************************/
  146. Function JumpSelect (){
  147. $ Url = $ this-> url;
  148. // Process URL parameters: the key of the array is the URL variable, and the value of the array is the value of the URL variable.
  149. Foreach ($ this-> Aque as $ key => $ val ){
  150. If ($ key! = "Page") $ Sque. = "& $ key =". $ this-> StrSift ($ val );
  151. }
  152. $ SelectStr = "\ n$ SelectStr. = "onChange = \" window. location = '". $ url ."? Page = '+ this. value +' ". $ Sque." '\ "> \ n ";For ($ I = 1; $ I <= $ this-> pages; $ I ++ ){If ($ I ==$ this-> page) $ selected = "selected ";Else $ selected = "";$ SelectStr. ="$ I\ N ";}$ SelectStr. ="\ N ";
  153. // Returns the paging string.
  154. Return $ SelectStr;
  155. }

  156. /**********************************

  157. * Filter special characters.
  158. ***********************************/
  159. Private function StrSift ($ str ){
  160. $ Str = str_replace ("\" "," ", $ str );
  161. $ Str = str_replace ("'", "", $ str );
  162. $ Str = str_replace ("[url = file: // % 22, % 22% 22, $ str/] \", "", $ str [/url]);
  163. $ Str = str_replace ("\/", "", $ str );
  164. $ Str = str_replace (":", "", $ str );
  165. $ Str = str_replace ("? "," ", $ Str "?? ".
  166. $ Str = str_replace (">", "", $ str );
  167. $ Str = str_replace ("<", "", $ str );
  168. $ Str = str_replace ("%", "", $ str );
  169. $ Str = str_replace ("*", "", $ str );
  170. $ Str = str_replace ("&", "", $ str );
  171. $ Str = str_replace (".", "", $ str );
  172. Return $ str;
  173. }

  174. // End the class

  175. }
  176. ?>

If you are interested, you can also refer to the following article: A good php paging class code and a practical php paging class

Pagination style sheet:

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.