Php paging code. you can set the paging mode in the "allow" mode.

Source: Internet
Author: User
Php paging code. you can set the paging mode in the "allow" mode.

  1. Class Page {

  2. Private $ total; // total quantity
  3. Private $ limit; // returns the mysql limit statement.
  4. Private $ pageStart; // the start value.
  5. Private $ pageStop; // The end value.
  6. Private $ pageNumber; // Number of pagination numbers displayed
  7. Private $ page; // current page
  8. Private $ pageSize; // The number displayed on each page
  9. Private $ pageToatl; // The total number of pages
  10. Private $ pageParam; // paging variable
  11. Private $ uri; // URL parameter
  12. /**
  13. * The pagination settings are case insensitive.
  14. * % PageToatl % // total number of pages
  15. * % Page % // current page
  16. * % PageSize % // number of data entries displayed on the current page
  17. * % PageStart % // start number of the current page
  18. * % PageStop % // number of end entries on the current page
  19. * % Total % // total number of data entries
  20. * % First % // home page
  21. * % Ending % // end page
  22. * % Up % // Previous Page
  23. * % Down % // Next page
  24. * % F % // Start Page
  25. * % E % // end page
  26. * % OmitFA % // skip before
  27. * % OmitEA % // skipped after adding jump
  28. * % OmitF % // omitted before
  29. * % OmitE % // later omitted
  30. * % NumberF % // fixed number of numeric pagination
  31. * % NumberD % // The right-side numeric paging
  32. * % Input % // jump to the input box
  33. * % GoTo % // jump button
  34. */Bbs.it-home.org
  35. Private $ pageType = '% page/% pageToatl % page % first % up % F % omitFA % numberF % omitEA % E % down % % ending % ';
  36. // Set the display value
  37. Private $ pageShow = array ('first '=> 'homepage', 'ending' => 'Last page', 'Up' => 'previous page ', 'Low' => 'next page', 'Goto '=> 'Go ');

  38. /**

  39. * Initialize the data and construct the data.
  40. * @ Access public
  41. * @ Param int $ total data volume
  42. * @ Param int $ pageSize number of entries displayed per page
  43. * @ Param int $ pageNumber the number displayed on the page (using % numberF % is different from using % numberD %)
  44. * @ Param string $ pageParam paging variable
  45. * @ Return void
  46. */
  47. Public function _ construct ($ total, $ pageSize = 10, $ pageNumber = 5, $ pageParam = 'p '){
  48. $ This-> total = $ total <0? 0: $ total;
  49. $ This-> pageSize = $ pageSize <0? 0: $ pageSize;
  50. $ This-> pageNumber = $ pageNumber <0? 0: $ pageNumber;
  51. $ This-> pageParam = $ pageParam;
  52. $ This-> calculate ();
  53. }

  54. /**

  55. * Display page
  56. * @ Access public
  57. * @ Return string HTML paging string
  58. */
  59. Public function pageShow (){
  60. $ This-> uri ();
  61. If ($ this-> pageToatl> 1 ){
  62. If ($ this-> page = 1 ){
  63. $ First = ''. $ this-> pageShow ['first'].'';
  64. $ Up = ''. $ this-> pageShow ['Up'].'';
  65. } Else {
  66. $ First = 'uri. '&'. $ this-> pageParam. '= 1 ">'. $ this-> pageShow ['first'].'';
  67. $ Up = 'uri. '&'. $ this-> pageParam. '= '. ($ this-> page-1 ). '"> '. $ this-> pageShow ['Up']. '';
  68. }
  69. If ($ this-> page >=$ this-> pageToatl ){
  70. $ Ending = ''. $ this-> pageShow ['ending'].'';
  71. $ Down = ''. $ this-> pageShow ['drop'].'';
  72. } Else {
  73. $ Ending = 'uri. '&'. $ this-> pageParam. '= '. $ this-> pageToatl. '"> '. $ this-> pageShow ['ending']. '';
  74. $ Down = 'uri. '&'. $ this-> pageParam. '= '. ($ this-> page + 1 ). '"> '. $ this-> pageShow ['low']. '';
  75. }
  76. $ Input ='';
  77. $ GoTo =''. $ This-> pageShow ['Goto'].'';
  78. } Else {
  79. $ First = ''; $ up =''; $ ending = ''; $ down =''; $ input = ''; $ GoTo = '';
  80. }
  81. $ This-> numberF ();
  82. Return str_ireplace (array ('% pageToatl %', '% page %', '% pageSize %', '% pageStart %', '% pageStop % ', '% total %', '% first %', '% ending %', '% up %', '% down %', '% input % ', '% GoTo %'), array ($ this-> pageToatl, $ this-> page, $ this-> pageStop-$ this-> pageStart, $ this-> pageStart, $ this-> pageStop, $ this-> total, $ first, $ ending, $ up, $ down, $ input, $ GoTo), $ this-> pageType );
  83. }

  84. /**

  85. * Digit paging
  86. */
  87. Private function numberF (){
  88. $ PageF = stripos ($ this-> pageType, '% numberF % ');
  89. $ PageD = stripos ($ this-> pageType, '% numberD % ');
  90. $ NumberF = ''; $ numberD =''; $ F = ''; $ E =''; $ omitF = ''; $ omitFA = ''; $ omitE = ''; $ omitEA = '';
  91. If ($ pageF! = False | $ pageD! = False ){
  92. If ($ pageF! = False ){
  93. $ Number = $ this-> pageNumber % 2 = 0? $ This-> pageNumber/2 :( $ this-> pageNumber + 1)/2;
  94. $ DStart = $ this-> page-$ number <0? $ This-> page-$ number-1: 0;
  95. If ($ this-> page + $ number-$ DStart> $ this-> pageToatl ){
  96. $ DStop = ($ this-> page + $ number-$ DStart)-$ this-> pageToatl;
  97. $ ForStop = $ this-> pageToatl + 1;
  98. } Else {
  99. $ DStop = 0;
  100. $ ForStop = $ this-> page + $ number-$ DStart + 1;
  101. }
  102. $ ForStart = $ this-> page-$ number-$ DStop <1? 1: $ this-> page-$ number-$ DStop;
  103. For ($ I = $ forStart; $ I <$ forStop; ++ $ I ){
  104. If ($ I ==$ this-> page ){
  105. $ NumberF. = ''. $ I .'';
  106. } Else {
  107. $ NumberF. = 'uri. '&'. $ this-> pageParam. '='. $ I. '">'. $ I .'';
  108. }
  109. }
  110. }
  111. If ($ pageD! = False ){
  112. $ Number = $ this-> pageNumber;
  113. $ ForStart = $ this-> page-$ number> 0? $ This-> page-$ number: 1;
  114. $ ForStop = $ this-> page + $ number> $ this-> pageToatl? $ This-> pageToatl + 1: $ this-> page + $ number + 1;
  115. For ($ I = $ forStart; $ I <$ this-> page; ++ $ I ){
  116. $ NumberD. = 'uri. '&'. $ this-> pageParam. '='. $ I. '">'. $ I .'';
  117. }
  118. $ NumberD. = ''. $ this-> page .'';
  119. $ Start = $ this-> page + 1;
  120. For ($ I = $ start; $ I <$ forStop; ++ $ I ){
  121. $ NumberD. = 'uri. '&'. $ this-> pageParam. '='. $ I. '">'. $ I .'';
  122. }
  123. }
  124. $ F = $ forStart> 1? 'Uri. '&'. $ this-> pageParam. '= 1 "> 1 ':'';
  125. $ E = $ forStop <$ this-> pageToatl + 1? 'Uri. '&'. $ this-> pageParam. '='. $ this-> pageToatl. '">'. $ this-> pageToatl .'':'';
  126. If ($ forStart> 2 ){
  127. $ OmitF = '... ';
  128. $ StartA = $ this-> page-$ number <1? 1: $ this-> page-$ number;
  129. $ OmitFA = 'uri. '&'. $ this-> pageParam. '='. $ startA. '">... ';
  130. }
  131. If ($ forStop <$ this-> pageToatl ){
  132. $ OmitE = '... ';
  133. $ StopA = $ this-> page + $ number> $ this-> pageToatl? $ This-> pageToatl: $ this-> page + $ number;
  134. $ OmitEA = 'uri. '&'. $ this-> pageParam. '='. $ stopA. '">... ';
  135. }
  136. }
  137. $ This-> pageType = str_ireplace (array ('% F %', '% E %', '% omitFA %', '% omitEA %', '% omitF % ', '% omitE %', '% numberF %', '% numberD %'), array ($ F, $ E, $ omitFA, $ omitEA, $ omitF, $ omitE, $ numberF, $ numberD), $ this-> pageType );
  138. }

  139. /**

  140. * Url processing method
  141. * @ Access public
  142. * @ Param array $ url: URL-preserving direct link array
  143. * @ Return string the url end parameter after filtering
  144. */
  145. Private function uri (){
  146. $ Url = $ _ SERVER ["REQUEST_URI"];
  147. $ Par = parse_url ($ url );
  148. If (isset ($ par ['query']) {
  149. Parse_str ($ par ['query'], $ query );
  150. If (! Is_array ($ this-> uri )){
  151. $ This-> uri = array ();
  152. }
  153. Array_merge ($ query, $ this-> uri );
  154. Unset ($ query [$ this-> pageParam]);
  155. While ($ key = array_search ('', $ query )){
  156. Unset ($ query [$ key]);
  157. }
  158. $ This-> uri = $ par ['path']. '? '. Http_build_query ($ query );
  159. } Else {
  160. $ This-> uri = $ par ['path']. '? ';
  161. }
  162. }

  163. /**

  164. * Set the limit method and calculate the number of start and end items.
  165. */
  166. Private function calculate (){
  167. $ This-> pageToatl = ceil ($ this-> total/$ this-> pageSize );
  168. $ This-> page = intval ($ _ GET [$ this-> pageParam]);
  169. $ This-> page = $ this-> page> = 1? $ This-> page> $ this-> pageToatl? $ This-> pageToatl: $ this-> page: 1;
  170. $ This-> pageStart = ($ this-> page-1) * $ this-> pageSize;
  171. $ This-> pageStop = $ this-> pageStart + $ this-> pageSize;
  172. $ This-> pageStop = $ this-> pageStop> $ this-> total? $ This-> total: $ this-> pageStop;
  173. $ This-> limit = $ this-> pageStart. ','. $ this-> pageStop;
  174. }

  175. /**

  176. * Set the filter.
  177. */
  178. Public function _ set ($ name, $ value ){
  179. Switch ($ name ){
  180. Case 'pagetype ':
  181. Case 'URL ':
  182. $ This-> $ name = $ value;
  183. Return;
  184. Case 'pageshow ':
  185. If (is_array ($ value )){
  186. $ This-> pageShow = array_merge ($ this-> pageShow, $ value );
  187. }
  188. Return;
  189. }
  190. }

  191. /**

  192. * Value filter
  193. */
  194. Public function _ get ($ name ){
  195. Switch ($ name ){
  196. Case 'limit ':
  197. Case 'pagestart ':
  198. Case 'pagestop ':
  199. Return $ this-> $ name;
  200. Default:
  201. Return;
  202. }
  203. }
  204. }

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.