Php paging principle and page jump instance

Source: Internet
Author: User
Php paging principle and page jump instance

  1. Select * from table limit 0, 10 // The first 10 records
  2. Select * from table limit 10, 10 // 11th to 20 records
  3. Select * from table limit 20, 10 // 21st to 30 records
  4. ......

This set of SQL statements is the SQL statement used to retrieve data of each page in the table when $ pagesize = 10. we can summarize this template: select * from table limit ($ currentpageid-1) * $ pagesize, $ pagesize takes this template into the corresponding value and compares it with the preceding SQL statement to see if this is the case. After solving the most important problem of how to obtain data, the rest is simply passing parameters, constructing appropriate SQL statements, and then using php to obtain data from the database and display it.

Php paging code:

  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. = '"; first page | 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. ?>

Oo-style code. the database connection in the following code is processed using the pear db class.

  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. ?>

Paging code call:

  1. // Filename: test_pager.php
  2. // This is a simple sample code. the code for establishing a database connection using the pear db class is omitted in the front.
  3. Require "pager. class. php ";
  4. If (isset ($ _ get ['Page'])
  5. {
  6. $ Page = (int) $ _ get ['Page'];
  7. }
  8. Else
  9. {
  10. $ Page = 1;
  11. }
  12. $ SQL = "select * from table order by id ";
  13. $ Pager_option = array (
  14. "SQL" => $ SQL,
  15. "Pagesize" => 10,
  16. "Currentpageid" => $ page
  17. );
  18. If (isset ($ _ get ['numitems '])
  19. {
  20. $ Pager_option ['numitems '] = (int) $ _ get ['numitems'];
  21. }
  22. $ Pager = @ new pager ($ pager_option );
  23. $ Data = $ pager-> getpagedata ();
  24. If ($ pager-> isfirstpage)
  25. {
  26. $ Turnover = "homepage | previous page | ";
  27. }
  28. Else
  29. {
  30. $ Turnover = "numitems." '> homepage | previouspageid. "& numitems =". $ pager-> numitems. "'> Previous Page | ";
  31. }
  32. If ($ pager-> islastpage)
  33. {
  34. $ Turnover. = "next page | last page ";
  35. }
  36. Else
  37. {
  38. $ Turnover. = "nextpageid. "& numitems = ". $ pager-> numitems. "'> Next Page | numpages. "& numitems = ". $ pager-> numitems. "'> last page ";
  39. }
  40. ?>

Note: This class only processes data and is not responsible for displaying the data, because it is barely possible to put the data processing and result display in a class. When the display status and requirements change, 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, to display the user page list, you can:

  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. ?>

Second, the compatibility of different databases may vary in the way a piece of results are intercepted in different databases.

  1. $ Pagenum = @ ceil ($ num/$ pagesize );
  2. If ($ num> $ pagesize ){
  3. If ($ pageval <= 1) $ pageval = 1;
  4. If ($ pageval ==$ pagenum ){
  5. Echo "previous page next page last page ";
  6. // Echo "no.". $ pageval. "page/total". $ pagenum. "page ";
  7. Echo "to\ N ";For ($ I = 1; $ I <= $ pagenum; $ I ++ ){If ($ I = $ pageval)Echo"$ I\ N ";ElseEcho"$ I\ N ";}Echo"Page, total $ pagenum page ";
  8. }
  9. }
  10. }
  11. ?>

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.