Encapsulated PHP paging class, easy to use

Source: Internet
Author: User
Encapsulated PHP paging class, easy to use
Class Pagination independent paging class
Call method:
$ Pagenation = new Pagination (4, 10,200); // 4 (first parameter) = currentPage, 10 (second parameter) = pageSize, 200 (third parameter) = total
$ Pagenation-> set_link ('http: // www.360hqb.com ');
$ Pagenation-> show ();

  1. /**
  2. * Class Pagination
  3. *
  4. * Independent paging class
  5. * Call method:
  6. * $ Pagenation = new Pagination (4, 10,200); // 4 (first parameter) = currentPage, 10 (second parameter) = pageSize, 200 (third parameter) = total
  7. * $ Pagenation-> set_link ('http: // www.360hqb.com ');
  8. * $ Pagenation-> show ();
  9. */
  10. Class Java_Pagination
  11. {
  12. Protected $ _ total = 0;
  13. Protected $ _ total_page = 0;
  14. Protected $ _ page = 1;
  15. Protected $ _ page_size = 10;
  16. Protected $ _ link = '';
  17. Protected $ _ grep = 3;
  18. Protected $ _ admin = false;
  19. Protected $ _ css_next = 'Next-page ';
  20. Protected $ _ css_prev = 'prev-page ';
  21. Protected $ _ css_curr = 'curr-page ';
  22. Protected $ _ css_page = 'Page-nav inline-block ';
  23. Public function _ construct ($ page, $ page_size, $ total, $ admin = false)
  24. {
  25. $ This-> set_current_page ($ page );
  26. $ This-> set_page_size ($ page_size );
  27. $ This-> set_total ($ total );
  28. If ($ admin)
  29. {
  30. $ This-> _ admin = $ admin;
  31. }
  32. // $ This-> _ link = $ _ SERVER ['request _ URI '];
  33. $ This-> set_link ($ _ SERVER ['request _ URI ']);
  34. }
  35. Public function set_link ($ link, $ is_shop = TRUE)
  36. {
  37. $ Len = strlen ($ link );
  38. $ Substr = substr ($ link, $ len-1 );
  39. If ('&' = $ substr)
  40. {
  41. $ Link = substr ($ link, 0, $ len-1 );
  42. }
  43. $ Pos = strpos ($ link ,'? ');
  44. If ($ pos)
  45. {
  46. $ Link = substr ($ link, 0, $ pos );
  47. }
  48. If (! Empty ($ _ GET ))
  49. {
  50. $ Link. = '? ';
  51. Foreach ($ _ GET as $ k => $ v)
  52. {
  53. If ('page' = strtolower ($ k ))
  54. {
  55. Continue;
  56. }
  57. $ Link. = $ k. '='. $ v .'&';
  58. }
  59. $ Len = strlen ($ link );
  60. $ Substr = substr ($ link, $ len-1 );
  61. If ('&' = $ substr)
  62. {
  63. $ Link = substr ($ link, 0, $ len-1 );
  64. }
  65. }
  66. Elseif (isset ($ _ SERVER ['query _ string']) AND! Empty ($ _ SERVER ['query _ string']) AND $ is_shop)
  67. {
  68. $ Link. = '? '. $ _ SERVER ['query _ string'];
  69. $ Len = strlen ($ link );
  70. $ Substr = substr ($ link, $ len-1 );
  71. If ('&' = $ substr)
  72. {
  73. $ Link = substr ($ link, 0, $ len-1 );
  74. }
  75. }
  76. $ This-> _ link = $ link;
  77. }
  78. Public function set_page_size ($ page_size)
  79. {
  80. If (empty ($ page_size ))
  81. {
  82. $ This-> _ page_size = 10;
  83. }
  84. Else
  85. {
  86. $ This-> _ page_size = (int) $ page_size;
  87. }
  88. }
  89. Public function set_total ($ total)
  90. {
  91. $ Page_size = empty ($ this-> _ page_size )? 10: $ this-> _ page_size;
  92. $ This-> _ total = $ total;
  93. If (0 = ($ total % $ page_size ))
  94. {
  95. $ This-> _ total_page = intval ($ total/$ page_size );
  96. }
  97. Else
  98. {
  99. $ This-> _ total_page = intval ($ total/$ page_size) + 1;
  100. }
  101. If ($ this-> _ page> $ this-> _ total_page)
  102. {
  103. $ This-> _ page = $ this-> _ total_page;
  104. }
  105. }
  106. Public function set_current_page ($ page)
  107. {
  108. If (empty ($ page ))
  109. {
  110. $ This-> _ page = 1;
  111. }
  112. Else
  113. {
  114. $ This-> _ page = (int) $ page;
  115. }
  116. }
  117. Public function get_next_page_btn ()
  118. {
  119. If ($ this-> _ page <$ this-> _ total_page)
  120. {
  121. $ Link = '';
  122. If (strpos ($ this-> _ link ,'? '))
  123. {
  124. $ Link = $ this-> _ link. '& page ='. ($ this-> _ page + 1 );
  125. }
  126. Else
  127. {
  128. $ Link = $ this-> _ link .'? Page = '. ($ this-> _ page + 1 );
  129. }
  130. If ($ this-> _ admin)
  131. {
  132. Return 'next page ';
  133. }
  134. Else
  135. {
  136. Return'
  137. Next page
  138. ';
  139. }
  140. }
  141. If ($ this-> _ admin)
  142. Return 'next page? ';
  143. Else
  144. Return '';
  145. }
  146. Public function get_prev_page_btn ()
  147. {
  148. If ($ this-> _ page> 1)
  149. {
  150. $ Link = '';
  151. If (strpos ($ this-> _ link ,'? '))
  152. {
  153. $ Link = $ this-> _ link. '& page ='. ($ this-> _ page-1 );
  154. }
  155. Else
  156. {
  157. $ Link = $ this-> _ link .'? Page = '. ($ this-> _ page-1 );
  158. }
  159. If ($ this-> _ admin)
  160. {
  161. Return 'previous page ';
  162. }
  163. Else
  164. {
  165. Return'
  166. Previous Page
  167. ';
  168. }
  169. }
  170. If ($ this-> _ admin)
  171. Return '? Previous Page ';
  172. Else
  173. Return '';
  174. }
  175. Public function get_current_page ()
  176. {
  177. If ($ this-> _ admin)
  178. Return''. $ This-> _ page .'';
  179. Else
  180. Return'
  181. '. $ This-> _ page .'
  182. ';
  183. }
  184. Public function get_page_link ($ page)
  185. {
  186. $ Link = '';
  187. If (strpos ($ this-> _ link ,'? '))
  188. {
  189. $ Link = $ this-> _ link. '& page ='. $ page;
  190. }
  191. Else
  192. {
  193. $ Link = $ this-> _ link .'? Page = '. $ page;
  194. }
  195. If ($ this-> _ admin)
  196. {
  197. Return ''. $ page .'';
  198. }
  199. Else
  200. {
  201. Return'
  202. '. $ Page .'
  203. ';
  204. }
  205. }
  206. Public function get_prev_pages ()
  207. {
  208. $ Pages = array ();
  209. $ Begin = $ this-> _ page-$ this-> _ grep;
  210. If ($ begin <1)
  211. {
  212. $ Begin = 1;
  213. }
  214. Elseif ($ begin> 2)
  215. {
  216. $ Pages [] = $ this-> get_page_link (1 );
  217. If ($ this-> _ admin)
  218. {
  219. $ Pages [] = '...';
  220. }
  221. Else
  222. {
  223. $ Pages [] ='
  224. ...
  225. ';
  226. }
  227. }
  228. Elseif ($ begin = 2)
  229. {
  230. $ Pages [] = $ this-> get_page_link (1 );
  231. }
  232. For ($ I = $ begin; $ I <$ this-> _ page; $ I ++)
  233. {
  234. $ Pages [] = $ this-> get_page_link ($ I );
  235. }
  236. Return $ pages;
  237. }
  238. Public function get_next_pages ()
  239. {
  240. $ Pages = array ();
  241. $ Begin = $ this-> _ page + 1;
  242. If ($ begin <$ this-> _ total_page)
  243. {
  244. $ End = $ begin + $ this-> _ grep;
  245. If ($ end> $ this-> _ total_page)
  246. {
  247. $ End = $ this-> _ total_page;
  248. }
  249. For ($ I = $ begin; $ I <$ end; $ I ++)
  250. {
  251. $ Pages [] = $ this-> get_page_link ($ I );
  252. }
  253. If ($ I <$ this-> _ total_page)
  254. {
  255. If ($ this-> _ admin)
  256. {
  257. $ Pages [] = '...';
  258. }
  259. Else
  260. {
  261. $ Pages [] ='
  262. ...
  263. ';
  264. }
  265. $ Pages [] = $ this-> get_page_link ($ this-> _ total_page );
  266. }
  267. Else
  268. {
  269. $ Pages [] = $ this-> get_page_link ($ this-> _ total_page );
  270. }
  271. }
  272. Elseif ($ begin ==$ this-> _ total_page)
  273. {
  274. $ Pages [] = $ this-> get_page_link ($ this-> _ total_page );
  275. }
  276. Return $ pages;
  277. }
  278. Public function show ()
  279. {
  280. If ($ this-> _ total_page <= 1)
  281. {
  282. Return;
  283. }
  284. If ($ this-> _ admin)
  285. {
  286. Echo'

    ';

  287. Echo 'total Records '. $ this-> _ total ';
  288. }
  289. Else
  290. {
  291. Echo'
      ';
    • }
    • Echo $ this-> get_prev_page_btn ();
    • $ Prev_pages = $ this-> get_prev_pages ();
    • If (! Empty ($ prev_pages ))
    • {
    • Foreach ($ prev_pages as $ page)
    • {
    • Echo $ page;
    • }
    • }
    • Echo $ this-> get_current_page ();
    • $ Next_pages = $ this-> get_next_pages ();
    • If (! Empty ($ next_pages ))
    • {
    • Foreach ($ next_pages as $ page)
    • {
    • Echo $ page;
    • }
    • }
    • Echo $ this-> get_next_page_btn ();
    • If ($ this-> _ admin)
    • {
    • Echo'

      ';
    • }
    • Else
    • {
    • Echo'
    ';
  292. }
  293. }
  294. }

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.