Detailed explanation of PHP paging code (with instance attached)

Source: Internet
Author: User
Detailed explanation of PHP paging code (with instance attached)

  1. // Establish a database connection

  2. $ Link = mysql_connect ("localhost", "mysql_user", "mysql_passWord ")
  3. Or die ("cocould not connect:". mysql_error ());
  4. // Obtain the current page number
  5. If (isset ($ _ GET ['Page']) {
  6. $ Page = intval ($ _ GET ['Page']);
  7. }
  8. Else {
  9. $ Page = 1;
  10. }
  11. // Number of pages per page
  12. $ PageSize = 10;
  13. // Obtain the total data volume
  14. $ SQL = "select count (*) as amount from table ";
  15. $ Result = mysql_query ($ SQL );
  16. $ Row = mysql_fetch_row ($ result );
  17. $ Amount = $ row ['amount'];
  18. // Count the total number of pages
  19. If ($ amount ){
  20. If ($ amount <$ page_size) {$ page_count = 1;} // if the total data volume is less than $ PageSize, there is only one page
  21. If ($ amount % $ page_size) {// Retrieve the total data volume divided by the remainder of each page
  22. $ Page_count = (int) ($ amount/$ page_size) + 1; // if there is a remainder, the number of pages equals the total data volume divided by the result of each page number and then adds one
  23. } Else {
  24. $ Page_count = $ amount/$ page_size; // if there is no remainder, the number of pages equals the total data volume divided by the result of each page
  25. }
  26. }
  27. Else {
  28. $ Page_count = 0;
  29. }
  30. // Flip link
  31. $ Page_string = '';
  32. If ($ page = 1 ){
  33. $ Page_string. = 'Page 1 | previous page | ';
  34. }
  35. Else {
  36. $ Page_string. = 'Page 1 | previous page | ';
  37. }
  38. If ($ page = $ page_count) | ($ page_count = 0 )){
  39. $ Page_string. = 'next page | last page ';
  40. }
  41. Else {
  42. $ Page_string. = 'next page | last page ';
  43. }
  44. // Obtain data and return results in two-dimensional array format
  45. If ($ amount ){
  46. $ SQL = "select * from table order by id desc limit". ($ page-1) * $ page_size. ", $ page_size ";
  47. $ Result = mysql_query ($ SQL );

  48. While ($ row = mysql_fetch_row ($ result )){

  49. $ Rowset [] = $ row;
  50. }
  51. } Else {
  52. $ Rowset = array ();
  53. }
  54. // The code that does not contain the display result is not in the scope of the discussion. you only need to use foreach to easily display the result using the two-dimensional array.
  55. ?>

4. use the pear db class to connect to the OO-style code database for processing.

  1. // FileName: Pager. class. php

  2. // Paging class. this class is only used to process the data structure and is not used to process the display.
  3. Class Pager
  4. {
  5. Var $ PageSize; // Number of pages per page
  6. Var $ CurrentPageID; // Current page number
  7. Var $ NextPageID; // Next page
  8. Var $ PreviousPageID; // Previous Page
  9. Var $ numPages; // The total number of pages.
  10. Var $ numItems; // The total number of records
  11. Var $ isFirstPage; // whether the first page is displayed
  12. Var $ isLastPage; // whether it is the last page
  13. Var $ SQL; // SQL query statement

  14. Function Pager ($ option)

  15. {
  16. Global $ db;
  17. $ This-> _ setOptions ($ option );
  18. // Total number of entries
  19. If (! Isset ($ this-> numItems ))
  20. {
  21. $ Res = $ db-> query ($ this-> SQL );
  22. $ This-> numItems = $ res-> numRows ();
  23. }
  24. // Total number of pages
  25. If ($ this-> numItems> 0)
  26. {
  27. If ($ this-> numItems <$ this-> PageSize) {$ this-> numPages = 1 ;}
  28. If ($ this-> numItems % $ this-> PageSize)
  29. {
  30. $ This-> numPages = (int) ($ this-> numItems/$ this-> PageSize) + 1;
  31. }
  32. Else
  33. {
  34. $ This-> numPages = $ this-> numItems/$ this-> PageSize;
  35. }
  36. }
  37. Else
  38. {
  39. $ This-> numPages = 0;
  40. }

  41. Switch ($ this-> CurrentPageID)

  42. {
  43. Case $ this-> numPages = 1:
  44. $ This-> isFirstPage = true;
  45. $ This-> isLastPage = true;
  46. Break;
  47. Case 1:
  48. $ This-> isFirstPage = true;
  49. $ This-> isLastPage = false;
  50. Break;
  51. Case $ this-> numPages:
  52. $ This-> isFirstPage = false;
  53. $ This-> isLastPage = true;
  54. Break;
  55. Default:
  56. $ This-> isFirstPage = false;
  57. $ This-> isLastPage = false;
  58. }

  59. If ($ this-> numPages> 1)

  60. {
  61. If (! $ This-> isLastPage) {$ this-> NextPageID = $ this-> CurrentPageID + 1 ;}
  62. If (! $ This-> isFirstPage) {$ this-> PreviousPageID = $ this-> CurrentPageID-1 ;}
  63. }

  64. Return true;

  65. }

  66. /***

  67. *
  68. * Database connection to the returned result set
  69. * When the result set is large, you can directly use this method to obtain the database connection and traverse the database outside of the class. this reduces the overhead.
  70. * If the result set is not large, you can directly use getPageData to obtain results in two-dimensional array format.
  71. * The getPageData method also calls this method to obtain the result.
  72. *
  73. ***/

  74. Function getDataLink ()

  75. {
  76. If ($ this-> numItems)
  77. {
  78. Global $ db;

  79. $ PageID = $ this-> CurrentPageID;

  80. $ From = ($ PageID-1) * $ this-> PageSize;

  81. $ Count = $ this-> PageSize;
  82. $ Link = $ db-> limitQuery ($ this-> SQL, $ from, $ count); // use the Pear DB: limitQuery method to ensure database compatibility

  83. Return $ link;

  84. }
  85. Else
  86. {
  87. Return false;
  88. }
  89. }

  90. /***

  91. *
  92. * Returns the result set in two-dimensional array format.
  93. *
  94. ***/

  95. Function getPageData ()

  96. {
  97. If ($ this-> numItems)
  98. {
  99. If ($ res = $ this-> getDataLink ())
  100. {
  101. If ($ res-> numRows ())
  102. {
  103. While ($ row = $ res-> fetchRow ())
  104. {
  105. $ Result [] = $ row;
  106. }
  107. }
  108. Else
  109. {
  110. $ Result = array ();
  111. }

  112. Return $ result;

  113. }
  114. Else
  115. {
  116. Return false;
  117. }
  118. }
  119. Else
  120. {
  121. Return false;
  122. }
  123. }

  124. Function _ setOptions ($ option)

  125. {
  126. $ Allow_options = array (
  127. 'Pagesize ',
  128. 'Currentpageid ',
  129. 'SQL ',
  130. 'Numitems'
  131. );

  132. Foreach ($ option as $ key => $ value)

  133. {
  134. If (in_array ($ key, $ allow_options) & ($ value! = Null ))
  135. {
  136. $ This-> $ key = $ value;
  137. }
  138. }

  139. Return true;

  140. }
  141. }
  142. ?>

  143. Call example:

  144. // FileName: test_pager.php
  145. // Omitted the code for using the pear db class to establish a database connection
  146. Require "Pager. class. php ";
  147. If (isset ($ _ GET ['Page'])
  148. {
  149. $ Page = (int) $ _ GET ['Page'];
  150. }
  151. Else
  152. {
  153. $ Page = 1;
  154. }
  155. $ SQL = "select * from table order by id ";
  156. $ Pager_option = array (
  157. "SQL" => $ SQL,
  158. "PageSize" => 10,
  159. "CurrentPageID" => $ page
  160. );
  161. If (isset ($ _ GET ['numitems '])
  162. {
  163. $ Pager_option ['numitems '] = (int) $ _ GET ['numitems'];
  164. }
  165. $ Pager = @ new Pager ($ pager_option );
  166. $ Data = $ pager-> getPageData ();
  167. If ($ pager-> isFirstPage)
  168. {
  169. $ Turnover = "homepage | previous page | ";
  170. }
  171. Else
  172. {
  173. $ Turnover = "numItems." '> homepage | PreviousPageID. "& numItems =". $ pager-> numItems. "'> Previous Page | ";
  174. }
  175. If ($ pager-> isLastPage)
  176. {
  177. $ Turnover. = "next page | last page ";
  178. }
  179. Else
  180. {
  181. $ Turnover. = "NextPageID. "& numItems = ". $ pager-> numItems. "'> Next Page | numPages. "& numItems = ". $ pager-> numItems. "'> last page ";
  182. }
  183. ?>

Note: This class only processes data and is not responsible for displaying the data, because I feel that it is a little stubborn to put the data processing and result display in a class. When the display status and requirements are changeable, it is better to handle the problem based on the results given by the class. the better way is to display different pages based on the Pager class inheriting a subclass of its own, for example, display the user paging list:

  1. Class MemberPager extends Pager

  2. {
  3. Function showMemberList ()
  4. {
  5. Global $ db;

  6. $ Data = $ this-> getPageData ();

  7. // Code for displaying the result
  8. //......
  9. }
  10. }
  11. /// Call
  12. If (isset ($ _ GET ['Page'])
  13. {
  14. $ Page = (int) $ _ GET ['Page'];
  15. }
  16. Else
  17. {
  18. $ Page = 1;
  19. }
  20. $ SQL = "select * from members order by id ";
  21. $ Pager_option = array (
  22. "SQL" => $ SQL,
  23. "PageSize" => 10,
  24. "CurrentPageID" => $ page
  25. );
  26. If (isset ($ _ GET ['numitems '])
  27. {
  28. $ Pager_option ['numitems '] = (int) $ _ GET ['numitems'];
  29. }
  30. $ Pager = @ new MemberPager ($ pager_option );
  31. $ Pager-> showMemberList ();
  32. ?>

Note: the compatibility of different databases varies with the way a piece of results are intercepted in different databases. Mysql: select * from table limit offset, rowspgsql: select * from table limit m offset n ...... therefore, when obtaining results in the class, you need to use the limitQuery method of the pear db class.

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.