A php paging code (compatible with url routing)

Source: Internet
Author: User
A php paging code (compatible with url routing)

  1. Class Page {
  2. Private $ total; // The total number of records in the data table
  3. Private $ listRows; // Number of Entries displayed on each page
  4. Private $ limit; // use the limit clause in SQL statements to limit the number of records to be retrieved
  5. Private $ uri; // automatically obtain the url request address
  6. Private $ pageNum; // The total number of pages.
  7. Private $ page; // current page
  8. Private $ config = array (
  9. 'Head' => 'records ',
  10. 'Prev' => 'previous page ',
  11. 'Next' => 'next page ',
  12. 'First' => 'homepage ',
  13. 'Last' => 'Last page'
  14. ); // Display the content in the page information. you can set it by using the set () method.
  15. Private $ listNum = 5; // Default number of display pages
  16. /**
  17. * Constructor: you can set the attributes of the paging class.
  18. * @ Param int $ total calculate the total number of records on the page
  19. * @ Param int $ listRows (optional) sets the number of records displayed on each page. the default value is 25.
  20. * @ Param mixed $ query (optional) it is an array or string format that passes parameters to the target page.
  21. * @ Param bool $ ord: The default value is true. the page is displayed from the first page, and false is displayed from the last page.
  22. */Bbs.it-home.org
  23. Public function _ construct ($ total, $ listRows = 25, $ query = '', $ ord = true ){
  24. $ This-> total = $ total;
  25. $ This-> listRows = $ listRows;
  26. $ This-> uri = $ this-> getUri ($ query );
  27. $ This-> pageNum = ceil ($ this-> total/$ this-> listRows );
  28. /* The following judgment is used to set the current page */
  29. If (! Empty ($ _ GET ['Page']) {
  30. $ Page = $ _ GET ['Page'];
  31. } Else {
  32. If ($ ord ){
  33. $ Page = 1;
  34. } Else {
  35. $ Page = $ this-> pageNum;
  36. }
  37. }
  38. If ($ total> 0 ){
  39. If (preg_match ('/\ D/', $ page )){
  40. $ This-> page = 1;
  41. } Else {
  42. $ This-> page = $ page;
  43. }
  44. }
  45. // $ This-> limit = 'limit'. $ this-> setLimit ();
  46. $ This-> limit = $ this-> setLimit ();
  47. }
  48. /**
  49. * Sets the display page information for consistent operations.
  50. * @ Param string $ the following table lists the attributes of the param member array config.
  51. * @ Param string $ value is used to set the element value corresponding to the config subscript.
  52. * @ Return object returns the current object $ this for consistent operations.
  53. */
  54. Function _ set ($ param, $ value ){
  55. If (array_key_exists ($ param, $ this-> config )){
  56. $ This-> config [$ param] = $ value;
  57. }
  58. // Return $ this;
  59. }
  60. /* You can directly obtain the limit and page values of private member attributes outside the object */
  61. Function _ get ($ args ){
  62. If ($ args = 'limit' | $ args = 'page '){
  63. Return $ this-> $ args;
  64. } Else {
  65. Return null;
  66. }
  67. }
  68. /**
  69. * Output pagination in the specified format
  70. * @ Param int 0-7 numbers are used as parameters to customize the output pagination structure and adjust the structure order. by default, all structures are output.
  71. * @ Return string paging information content
  72. */
  73. Function fpage (){
  74. $ Arr = func_get_args ();
  75. $ Html [0] = "total{$ This-> total}{$ This-> config ['head']} ";
  76. $ Html [1] = "this page". $ This-> disnum ()."";
  77. $ Html [2] = "this page is from{$ This-> start ()}-{$ this-> end ()}";
  78. $ Html [3] ="{$ This-> page}/{$ this-> pageNum}Page ";
  79. $ Html [4] = $ this-> firstprev ();
  80. $ Html [5] = $ this-> pageList ();
  81. $ Html [6] = $ this-> nextlast ();
  82. $ Html [7] = $ this-> goPage ();
  83. $ Fpage ='

    ';

  84. If (count ($ arr) <1 ){
  85. $ Arr = array (0, 1, 2, 3, 4, 5, 6, 7 );
  86. }
  87. For ($ I = 0; $ I $ Fpage. = $ html [$ arr [$ I];
  88. }
  89. $ Fpage. ='

    ';
  90. Return $ fpage;
  91. }
  92. /* Used inside the object to automatically obtain the current url for access */
  93. Private function getUri ($ query ){
  94. $ Request_uri = $ _ SERVER ['request _ URI '];
  95. $ Url = strstr ($ request_uri ,'? ')? $ Request_uri: $ request_uri .'? ';
  96. If (is_array ($ query )){
  97. $ Url. = http_build_query ($ query );
  98. } Else if ($ query! = ''){
  99. $ Url. = '&'. trim ($ query ,'? &');
  100. }
  101. $ Arr = parse_url ($ url );
  102. If (isset ($ arr ['query']) {
  103. Parse_str ($ arr ['query'], $ arrs );
  104. Unset ($ arrs ['Page']);
  105. $ Url = $ arr ['path']. '? '. Http_build_query ($ arrs );
  106. }
  107. If (strstr ($ url ,'? ')){
  108. If (substr ($ url,-1 )! = '? '){
  109. $ Url = $ url .'&';
  110. }
  111. } Else {
  112. $ Url. = '? ';
  113. }
  114. Return $ url;
  115. }
  116. /* Private method, set limit */
  117. Private function setLimit (){
  118. If ($ this-> page> 0 ){
  119. Return ($ this-> page-1) * $ this-> listRows. ", {$ this-> listRows }";
  120. } Else {
  121. Return 0;
  122. }
  123. }
  124. /* Private method used inside the object to obtain the number of records starting from the current page */
  125. Private function start (){
  126. If ($ this-> total = 0 ){
  127. Return 0;
  128. } Else {
  129. Return ($ this-> page-1) * $ this-> listRows + 1;
  130. }
  131. }
  132. /* Used to obtain the number of records at the end of the current page */
  133. Private function end (){
  134. Return min ($ this-> page * $ this-> listRows, $ this-> total );
  135. }
  136. /* Used to obtain the number of records displayed on this page */
  137. Private function disnum (){
  138. If ($ this-> total> 0 ){
  139. Return $ this-> end ()-$ this-> start () + 1;
  140. } Else {
  141. Return 0;
  142. }
  143. }
  144. /* Used to obtain operation information of the previous page and homepage */
  145. Private function firstprev (){
  146. If ($ this-> page> 1 ){
  147. $ Str = "uri} page = 1' >{$ this-> config ['first']}";
  148. $ Str. = "uri} page =". ($ this-> page-1). "'>{$ this-> config ['prev']}";
  149. Return $ str;
  150. }
  151. }
  152. Private function pageList (){
  153. $ LinkPage ='';
  154. $ PageSub = $ this-> page % $ this-> listNum;
  155. If ($ pageSub = 0 & $ this-> page> 0 ){
  156. $ PageSub = $ this-> listNum;
  157. }
  158. /* List at the front of the current page */
  159. For ($ I = $ pageSub-1; $ I >=1; $ I --){
  160. $ Page = $ this-> page-$ I;
  161. If ($ page> = 1 ){
  162. $ LinkPage. = "uri} page = {$ page} '> {$ page }";
  163. }
  164. }
  165. /* Information on the current page */
  166. If ($ this-> pageNum> 1 ){
  167. $ LinkPage. = "{$ this-> page }";
  168. }
  169. /* List after the current page */
  170. For ($ I = 1; $ I <= $ this-> listNum-$ pageSub; $ I ++ ){
  171. $ Page = $ this-> page + $ I;
  172. If ($ page <= $ this-> pageNum ){
  173. $ LinkPage. = "uri} page = {$ page} '> {$ page }";
  174. } Else {
  175. Break;
  176. }
  177. }
  178. $ LinkPage. ='';
  179. Return $ linkPage;
  180. }
  181. /* Used to obtain page number list information */
  182. Private function pageListBak (){
  183. $ LinkPage ='';
  184. $ Inum = floor ($ this-> listNum/2 );
  185. /* List at the front of the current page */
  186. For ($ I = $ inum; $ I >=1; $ I --){
  187. $ Page = $ this-> page-$ I;
  188. If ($ page> = 1 ){
  189. $ LinkPage. = "uri} page = {$ page} '> {$ page }";
  190. }
  191. }
  192. /* Information on the current page */
  193. If ($ this-> pageNum> 1 ){
  194. $ LinkPage. = "{$ this-> page }";
  195. }
  196. /* List after the current page */
  197. For ($ I = 1; $ I <= $ inum; $ I ++ ){
  198. $ Page = $ this-> page + $ I;
  199. If ($ page <= $ this-> pageNum ){
  200. $ LinkPage. = "uri} page = {$ page} '> {$ page }";
  201. } Else {
  202. Break;
  203. }
  204. }
  205. $ LinkPage. ='';
  206. Return $ linkPage;
  207. }
  208. /* Obtain the operation information of the next and last pages */
  209. Private function nextlast (){
  210. If ($ this-> page! = $ This-> pageNum ){
  211. $ Str = "uri} page =". ($ this-> page + 1). "'>{$ this-> config ['Next']}";
  212. $ Str. = "uri} page = {$ this-> pageNum} '> {$ this-> config ['last']}";
  213. Return $ str;
  214. }
  215. }
  216. /* Display and process the form jump page */
  217. Private function goPage (){
  218. If ($ this-> pageNum> 1 ){
  219. Return'';
  220. }
  221. }
  222. }

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.