Php paging code. you can customize the php paging code parameter.

Source: Internet
Author: User
Php paging code. you can customize the php paging code parameter.

  1. Class PageLink {

  2. /* Page display parameter settings */
  3. Private $ db_table = "";
  4. Private $ db_table_field = ""; // fields in the database to be displayed
  5. Private $ condition = ""; // query condition
  6. Private $ sort = ""; // sorting condition
  7. Private $ page_size = 0; // number of records displayed on each page
  8. Private $ link_num = 0; // display the number of page number links
  9. Private $ page = 1; // Current page number
  10. Private $ records = 0; // total number of records in the table
  11. Private $ page_count = 0; // total number of pages
  12. Private $ pagestring = ""; // string of the front and back pagination links
  13. Private $ linkUrl = ""; // Current page path
  14. Private $ urlPara = ""; // url parameters of the current page
  15. Private $ linkUrlNum = 1;
  16. /*
  17. * Custom paging
  18. */
  19. Private $ linkFormat = "";

  20. /* Obtained data */

  21. Private $ page_data = ""; // data obtained from the database,

  22. // $ Page_data is received as a two-dimensional array

  23. Private $ dbHelper;

  24. /* Variable definition part end */

  25. /* Function definition (class method) begin */

  26. Function _ construct () {include_once 'dbhelper/DBHelper. php'; // This is a database operation class

  27. $ This-> dbHelper = new DBHelper ();
  28. }

  29. /*

  30. * Set page information begin
  31. * @ Param $ db_table
  32. * @ Param $ db_table_field
  33. * @ Param $ condition
  34. * @ Param $ sort
  35. * @ Param $ page_size displays the number of conditions.
  36. * @ Param $ link_num number of data records
  37. * @ Param $ url router interface
  38. * @ Param $ para url parameters
  39. */
  40. Public function set ($ db_table, $ db_table_field, $ condition, $ sort, $ page_size, $ link_num, $ url, $ para ){
  41. $ This-> db_table = $ db_table; // table name
  42. $ This-> db_table_field = $ db_table_field; // field array,
  43. $ This-> condition = $ condition; // sorting condition
  44. $ This-> sort = $ sort; // sorting condition
  45. // Name of the field to be displayed
  46. // Write the array
  47. /* Set the end of the db parameter */

  48. /* Set the paging parameter to begin */

  49. $ This-> page_size = $ page_size; // number of records displayed on each page
  50. $ This-> link_num = $ link_num; // display the number of paging links
  51. $ This-> linkUrl = $ url;
  52. $ This-> urlPara = $ para;
  53. /* Set end with paging parameters */
  54. }

  55. /* Set the end of the page information */

  56. /* Obtain the paging link data begin */

  57. Public function get (){

  58. $ Page_data [0] = $ this-> pagestring;
  59. $ Page_data [1] = $ this-> page_data;
  60. Return $ page_data;
  61. }

  62. /* Get the end of the paging link data */

  63. /* Page number processing begin */

  64. Private function set_page (){

  65. If (isset ($ _ REQUEST ["page"]) {
  66. $ This-> page = intval ($ _ REQUEST ["page"]);
  67. } Else {
  68. $ This-> page = 1;
  69. }
  70. }

  71. /* Page number processing end */

  72. /* Obtain the number of records in the database begin */

  73. Private function get_records (){

  74. $ This-> records = $ this-> dbHelper-> counts ($ this-> db_table, $ this-> condition );
  75. }

  76. /* Obtain the number of records in the database. end */

  77. /* Create a flip link string begin */

  78. Private function page_link (){

  79. $ CheckPage = intval ($ this-> page/$ this-> link_num );
  80. $ StartPage = 1;
  81. $ StopPage = 1;
  82. If ($ checkPage = 0 & $ this-> page <$ this-> link_num ){
  83. $ StartPage = 1;
  84. }
  85. $ LinkPage = ($ this-> link_num/2 );
  86. If ($ this-> page> $ linkPage ){
  87. $ StartPage = $ this-> page-$ linkPage;
  88. }
  89. If ($ startPage + $ this-> link_num)> $ this-> page_count ){
  90. $ StartPage = $ this-> page_count-$ this-> link_num + 1;
  91. }
  92. If ($ startPage <1 ){
  93. $ StartPage = 1;
  94. }
  95. $ StopPage = $ startPage + ($ this-> link_num-1 );
  96. If ($ stopPage> $ this-> page_count ){
  97. $ StopPage = $ this-> page_count;
  98. }
  99. $ CountStr = "total". $ this-> records. "note ";
  100. $ CurrStr ="". $ This-> page ."/". $ This-> page_count." success ";
  101. $ BeginLink = "linkUrl ."? Page = 1 ". $ this-> urlPara." '> first vertex ";
  102. $ PreLink = "linkUrl ."? Page = ". ($ this-> page-1). $ this-> urlPara." '> previous partition ";
  103. $ NextLink = "linkUrl ."? Page = ". ($ this-> page + 1). $ this-> urlPara." '> next cursor ";
  104. $ NoPreLink = "previous release ";
  105. $ NoNextLink = "Next release ";
  106. $ EndLink = "linkUrl ."? Page = ". $ this-> page_count. $ this-> urlPara." '> tail cursor ";
  107. If ($ this-> page> ($ linkPage + 1) & $ this-> page_count> $ this-> link_num ){
  108. $ CurrPage. = "linkUrl ."? Page = 1 ". $ this-> urlPara." '> 1 ";
  109. }
  110. For ($ I = $ startPage; $ I <= $ stopPage; $ I ++ ){
  111. If ($ I ==$ this-> page ){
  112. $ CurrPage. = "". $ I ."";
  113. }
  114. Else {
  115. $ CurrPage. = "linkUrl ."? Page = ". $ I. $ this-> urlPara." '> ". $ I ."";
  116. }
  117. }
  118. If ($ this-> page_count-$ this-> page)> $ linkPage & $ this-> page_count> $ this-> link_num ){
  119. $ CurrPage. = "... $ this-> linkUrl ."? Page = ". $ this-> page_count. $ this-> urlPara." '> ". $ this-> page_count ."";
  120. }
  121. $ JumpPage. ="";
  122. $ JumpPage. ="LinkUrl
  123. ."? Page = '+ document. getElementById ('page _ text _ ". $ this-> linkUrlNum."'). value + '". $ this-> urlPara
  124. . "'\" Name = 'page _ submit' value = 'go'/> ";
  125. $ This-> linkUrlNum ++;
  126. If (! Empty ($ this-> linkFormat )){
  127. $ TempLinkFormat = $ this-> linkFormat;
  128. $ TempLinkFormat = str_replace ("president", $ countStr, $ tempLinkFormat );
  129. $ TempLinkFormat = str_replace ("repeated times", $ currStr, $ tempLinkFormat );
  130. If ($ this-> page_count> 1 ){
  131. $ TempLinkFormat = str_replace ("first vertex", $ beginLink, $ tempLinkFormat );
  132. If ($ this-> page> 1 ){
  133. $ TempLinkFormat = str_replace ("last updated", $ preLink, $ tempLinkFormat );
  134. }
  135. Else {
  136. $ TempLinkFormat = str_replace ("last updated", $ noPreLink, $ tempLinkFormat );
  137. }
  138. If ($ this-> page <$ this-> page_count ){
  139. $ TempLinkFormat = str_replace ("Next release", $ nextLink, $ tempLinkFormat );
  140. }
  141. Else {
  142. $ TempLinkFormat = str_replace ("Next release", $ noNextLink, $ tempLinkFormat );
  143. }
  144. $ TempLinkFormat = str_replace ("last modified", $ endLink, $ tempLinkFormat );
  145. $ TempLinkFormat = str_replace ("minute into", $ currPage, $ tempLinkFormat );
  146. $ TempLinkFormat = str_replace ("skip", $ jumpPage, $ tempLinkFormat );
  147. }
  148. Else {
  149. $ TempLinkFormat = str_replace ("first digit", "", $ tempLinkFormat );
  150. $ TempLinkFormat = str_replace ("last updated", "", $ tempLinkFormat );
  151. $ TempLinkFormat = str_replace ("Next region", "", $ tempLinkFormat );
  152. $ TempLinkFormat = str_replace ("tailslate", "", $ tempLinkFormat );
  153. $ TempLinkFormat = str_replace ("minute", "", $ tempLinkFormat );
  154. $ TempLinkFormat = str_replace ("skip", "", $ tempLinkFormat );
  155. }

  156. }

  157. $ This-> pagestring. = $ countStr. "". $ currStr;
  158. If ($ this-> page_count> 1 ){
  159. $ This-> pagestring. = $ beginLink;
  160. If ($ this-> page> 1 ){
  161. $ This-> pagestring. = $ preLink;
  162. }
  163. $ This-> pagestring. = $ currPage;
  164. If ($ this-> page <$ this-> page_count ){
  165. $ This-> pagestring. = $ nextLink;
  166. }
  167. $ This-> pagestring. = $ endLink. $ jumpPage;
  168. }
  169. }

  170. /* Create a flip link string end */

  171. /* Get data begin */

  172. Private function fetch_data (){

  173. If ($ this-> records ){
  174. $ Limit = ($ this-> page-1) * $ this-> page_size. ", $ this-> page_size ";
  175. $ This-> page_data = $ this-> dbHelper-> fetch ($ this-> db_table, $ this-> condition, $ this-> sort, $ limit );
  176. }
  177. }

  178. /* Get data end */

  179. /* Create a page begin */

  180. Public function create_page (){

  181. $ This-> set_page ();
  182. $ This-> get_records ();
  183. $ This-> page_count = ceil ($ this-> records/$ this-> page_size );
  184. $ This-> page_link ();
  185. $ This-> fetch_data ();
  186. }

  187. /* Create a paging end */

  188. Function _ destruct (){

  189. }

  190. /* Function definition (class method) end */

  191. }
  192. /*
  193. Call method
  194. Include_once 'pagelink. php ';
  195. $ PageLink = new PageLink (); // instantiate an object
  196. $ PageLink-> set ("table", "* (or field)", "condition", "sort", "number of data entries", "number of links per page ", "page (list. php) ","? Other parameters except page = 1 (& id = 1 & name = test) "); // input parameters
  197. $ PageLink-> create_page (); // Create a page
  198. $ List = $ pageLink-> get (); // get Data
  199. Echo $ list [0]; // paging link
  200. Print_r ($ list [1]); // Print the data, or loop it out as needed
  201. */

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.