Typical php paging code

Source: Internet
Author: User
Typical php paging code

  1. /**************************************
  2. File: class. paging. php
  3. Php paging code
  4. **************************************/
  5. Class paging {
  6. Var $ SQL _results;
  7. Var $ SQL _query;
  8. Var $ display_offset;
  9. Var $ display_limit;
  10. Var $ display_limit_URI;
  11. Var $ display_currentpage;
  12. Var $ display_totalpages;
  13. Var $ alt_content_count;
  14. Var $ display_offset_var;
  15. Var $ display_limit_var;
  16. Var $ display_mid_offset;
  17. Var $ display_link_first;
  18. Var $ display_link_prev;
  19. Var $ display_link_next;
  20. Var $ display_link_last;
  21. Var $ page_filename;
  22. Var $ page_uri;
  23. Var $ content;
  24. Var $ content_type;
  25. Var $ content_count;
  26. # CONSTRUCTOR
  27. Function paging ($ offsetVar = 'offset', $ limit = 10, $ midOffset = 2 ){
  28. $ This-> display_offset = isset ($ _ REQUEST [$ offsetVar])? $ _ REQUEST [$ offsetVar]: 0;
  29. $ This-> display_limit = is_numeric ($ limit )? $ Limit: 10;
  30. $ This-> display_mid_offset = $ midOffset;
  31. $ This-> display_offset_var = $ offsetVar;
  32. $ This-> display_currentpage = ceil ($ this-> display_offset/$ this-> display_limit );
  33. }
  34. # HANDLE DATA
  35. Function setContent ($ content ){
  36. If (is_array ($ content ))
  37. $ This-> content_type = 'array ';
  38. $ This-> content = $ content;
  39. $ This-> private_contentDetails ();
  40. $ This-> getLinks ();
  41. }
  42. Function private_contentDetails (){
  43. If ($ this-> content_type = 'array ')
  44. $ This-> content_count = count ($ this-> content );
  45. }
  46. Function getContent (){
  47. $ ReturnAry = array ();
  48. For (
  49. $ I = $ this-> display_offset;
  50. $ I <min ($ this-> content_count, $ this-> display_offset + $ this-> display_limit );
  51. ++ $ I
  52. )
  53. Array_push ($ returnAry, $ this-> content [$ I]);
  54. Return $ returnAry;
  55. }
  56. # LINKS TO NAVIGATE
  57. Function getLinks (){
  58. $ This-> getNextLink ('');
  59. $ This-> getFirstLink ('');
  60. $ This-> getLastLink ('');
  61. $ This-> getPrevLink ('');
  62. }
  63. Function getNext (){
  64. Return $ this-> display_link_next;
  65. }
  66. Function getPrev (){
  67. Return $ this-> display_link_prev;
  68. }
  69. Function getFirst (){
  70. Return $ this-> display_link_first;
  71. }
  72. Function getLast (){
  73. Return $ this-> display_link_last;
  74. }
  75. Function getNextLink ($ caption ){
  76. // Check if we're counting content or the alternate user provided count
  77. If ($ this-> alt_content_count)
  78. $ Count = $ this-> alt_content_count;
  79. Else
  80. $ Count = $ this-> content_count;
  81. If ($ this-> display_offset + $ this-> display_limit-$ count> = 0 ){
  82. $ This-> display_link_next = $ this-> display_offset;
  83. } Else {
  84. $ This-> display_link_next = min (
  85. $ Count-1,
  86. $ This-> display_offset + $ this-> display_limit
  87. );
  88. }
  89. Return $ this-> buildLink ($ this-> buildNewURI ($ this-> display_link_next), $ caption );
  90. }
  91. Function getPrevLink ($ caption ){
  92. $ This-> display_link_prev = max (
  93. 0,
  94. $ This-> display_offset-$ this-> display_limit
  95. );
  96. Return $ this-> buildLink ($ this-> buildNewURI ($ this-> display_link_prev), $ caption );
  97. }
  98. Function getFirstLink ($ caption ){
  99. $ This-> display_link_first = 0;
  100. Return $ this-> buildLink ($ this-> buildNewURI ($ this-> display_link_first), $ caption );
  101. }
  102. Function getLastLink ($ caption ){
  103. $ This-> display_link_last = $ this-> content_count-$ this-> display_limit;
  104. Return $ this-> buildLink ($ this-> buildNewURI ($ this-> display_link_last), $ caption );
  105. }
  106. # NUMBERS
  107. Function getPageCount (){
  108. If ($ this-> alt_content_count)
  109. $ Count = $ this-> alt_content_count;
  110. Else
  111. $ Count = $ this-> content_count;
  112. $ This-> display_totalpages = ceil (
  113. $ Count/$ this-> display_limit
  114. );
  115. Return $ this-> display_totalpages;
  116. }
  117. Function getPageNumbers (){
  118. // Define variables
  119. $ MidOffset = $ this-> display_mid_offset;
  120. $ FirstLink = $ this-> display_link_first;
  121. $ PageCount = $ this-> getPageCount ();
  122. $ ThisPage = $ this-> display_currentpage;
  123. $ Limit = $ this-> display_limit;
  124. $ Displayed = $ midOffset * 2 + 1;
  125. $ ReturnAry = array ();
  126. // Dots
  127. $ ReturnAry ['afterdot '] = '';
  128. $ ReturnAry ['beforedot '] = '';
  129. // If two times and center the number is less than all the pages together
  130. If ($ midOffset * 2 + 1) <$ pageCount ){
  131. $ Min = max ($ firstLink, $ thisPage-$ midOffset );
  132. $ Max = min ($ pageCount, $ thisPage + $ midOffset + 1 );
  133. $ Total = $ max-$ min;
  134. // This keeps x amount of pages displayed even if
  135. // You're not in mid cause of page 1 or 2
  136. If ($ total <$ displayed ){
  137. If ($ min = 0 ){
  138. $ Max + = $ displayed-$ total;
  139. }
  140. If ($ max = $ pageCount ){
  141. $ Min-= $ displayed-$ total;
  142. }
  143. }
  144. } Else {
  145. # Just output a set of numbers
  146. $ Min = 0;
  147. $ Max = $ pageCount;
  148. }
  149. // Run pages, check for current page and name it
  150. For ($ I = $ min; $ I <$ max; ++ $ I ){
  151. $ Cp = 'no ';
  152. If ($ thisPage = $ I)
  153. $ Cp = 'yes ';
  154. If ($ max! = $ PageCount)
  155. $ ReturnAry ['afterdot '] = '...';
  156. If ($ min> 0)
  157. $ ReturnAry ['foreud'] = '...';
  158. Array_push ($ returnAry,
  159. Array (
  160. 'Currentpage' => $ cp,
  161. 'Pagenumber' => ($ I + 1 ),
  162. 'Pagelink '=> $ this-> buildLink ($ this-> buildNewURI ($ I * $ limit), ($ I + 1 ))
  163. )
  164. );
  165. }
  166. Return $ returnAry;
  167. }
  168. Function makePageNumbers ($ format, $ pages, $ boldCurrent = true, $ separator = ''){
  169. $ RetPages = '';
  170. // Make actual page numbers
  171. Foreach ($ pages as $ key => $ value ):
  172. If (is_numeric ($ key ))
  173. $ RetPages. = ('yes' = $ value ['currentpage'] & $ boldCurrent )? ''. $ Value ['pagelink'].''. $ Separator: $ value ['pagelink']. $ separator;
  174. Endforeach;
  175. $ Format = str_replace (array ('{beforedot }',
  176. '{Afterdot }',
  177. '{Pages }',
  178. '{Separator }'),
  179. Array ($ pages ['beforedot '],
  180. $ Pages ['afterdot '],
  181. $ RetPages ),
  182. $ Format );
  183. Return $ format;
  184. }
  185. # CHECKS
  186. Function isFirstPage (){
  187. If ($ this-> display_currentpage = 0)
  188. Return true;
  189. Return false;
  190. }
  191. Function isLastPage (){
  192. // Add one because basis is 0, not 1
  193. If ($ this-> display_currentpage + 1 = $ this-> getPageCount ())
  194. Return true;
  195. Return false;
  196. }
  197. # FUNCTIONS
  198. Function getPageName (){
  199. $ FileName = explode ('/', $ _ SERVER ['request _ URI ']);
  200. $ FileName = $ fileName [count ($ fileName)-1];
  201. If (strpos ($ fileName ,'? ')> 0 ){
  202. $ FileName = explode ('? ', $ FileName );
  203. $ FileName = $ fileName [0];
  204. }
  205. Return $ fileName;
  206. }
  207. Function getCleanURI (){
  208. $ URI = $ _ SERVER ['request _ URI '];
  209. If (strpos ($ URI ,'? ')> 0 ){
  210. $ URI = explode ('? ', $ URI );
  211. $ URI = $ URI [1];
  212. $ URI = preg_replace ('/\ B'. $ this-> display_offset_var. '\ B [= 0-9 &] {2, 20}/', '', $ URI );
  213. // $ URI = preg_replace ('/\ B '. $ this-> display_limit_var. '\ B [= 0-9 &] {2, 20}/', '', $ URI );
  214. Return $ URI;
  215. }
  216. Return false;
  217. }
  218. Function buildNewURI ($ offset ){
  219. $ NewFile = $ this-> getPageName ().'? '. $ This-> getCleanURI ();
  220. $ LastChar = substr ($ newFile, strlen ($ newFile)-1, 1 );
  221. If ($ lastChar! = '& $ LastChar! = '? ')
  222. $ NewFile. = '&';
  223. $ NewFile. = $ this-> display_offset_var. '='. $ offset .'&';
  224. // $ NewFile. = $ this-> display_limit_var. '='. $ limit .'&';
  225. Return $ newFile;
  226. }
  227. Function buildLink ($ href, $ caption, $ target = NULL ){
  228. If ($ target! = NULL)
  229. $ Target = 'target = "'. $ target .'"';
  230. Return ''. $ caption .'';
  231. }
  232. Function encodeURI ($ str ){
  233. $ Salt = 'falaful ';
  234. $ Str = strrev ($ salt. $ str );
  235. Return base64_encode ($ str );
  236. }
  237. Function decodeURI ($ str ){
  238. $ Salt = 'falaful ';
  239. $ Str = strrev (base64_decode ($ str ));
  240. $ Str = substr ($ str, strlen ($ salt), strlen ($ str ));
  241. Return $ str;
  242. }
  243. ##############
  244. #
  245. # These functions are for inputting a query for this
  246. # Class to execute. The other functions will grab all
  247. # X amount of rows then truncate it. These functions will
  248. # Only limit to whatever amount. This improves performance.
  249. # Reason is so you dont grab 1,000,000 rows when you want 3.
  250. #
  251. ##############
  252. Function runWQuery ($ db, $ statement, $ table, $ where = ''){
  253. // Get total rows
  254. $ Db-> query ('SELECT COUNT (*) AS count FROM '. $ table. ''. $ where );
  255. $ Db-> movenext ();
  256. $ This-> alt_content_count = $ db-> col ['count'];
  257. // Add limit to query
  258. $ Where. = 'limit '. $ this-> display_offset.', '. $ this-> display_limit;
  259. // Save query
  260. $ This-> SQL _query = $ statement. 'from'. $ table. ''. $ where;
  261. // Print query
  262. // Echo $ this-> SQL _query;
  263. // Run query
  264. $ Db-> query ($ this-> SQL _query );
  265. $ This-> SQL _results = array ();
  266. // Save results
  267. While ($ db-> movenext ()){
  268. Array_push ($ this-> SQL _results, $ db-> col );
  269. }
  270. Return $ this-> runQueryActions ();
  271. }
  272. Function runQueryActions (){
  273. $ This-> setContent ($ this-> SQL _results );
  274. $ Pages = $ this-> getPageNumbers ();
  275. // Make actual page numbers
  276. $ RetPages = $ this-> makePageNumbers ('{beforedot} {pages} {afterdot}', $ pages, true ,'');
  277. // Get new display
  278. Return array (
  279. 'Content' => $ this-> SQL _results,
  280. 'Pages '=> $ retPages
  281. );
  282. }
  283. }
  284. ?>

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.