One php + mysql paging code

Source: Internet
Author: User
One php + mysql paging code

  1. /************************************

  2. Class name: pagesupport
  3. Function: displays data in the mysql database by page.
  4. *************************************/
  5. Class pagesupport {
  6. // Attributes
  7. Var $ SQL; // SQL query statement of the data to be displayed
  8. Var $ page_size; // maximum number of lines per page
  9. Var $ start_index; // The sequence number of the first row of the record to be displayed
  10. Var $ total_records; // The total number of records.
  11. Var $ current_records; // number of records read on the current page
  12. Var $ result; // read the result
  13. Var $ total_pages; // total number of pages
  14. Var $ current_page; // Current page number
  15. Var $ display_count = 30; // The first and last pages displayed
  16. Var $ arr_page_query; // array, which contains the parameters to be passed for pagination
  17. Var $ first;
  18. Var $ prev;
  19. Var $ next;
  20. Var $ last;
  21. // Method
  22. /*************************************** ******
  23. Construct ()
  24. Input parameters:
  25. $ Ppage_size: maximum number of lines per page
  26. **************************************** *******/
  27. Function PageSupport ($ ppage_size)
  28. {
  29. $ This-> page_size = $ ppage_size;
  30. $ This-> start_index = 0;
  31. }

  32. /*************************************** ******

  33. Constructor :__ destruct ()
  34. Input parameters:
  35. **************************************** *******/
  36. Function _ destruct ()
  37. {
  38. }
  39. /*************************************** ******
  40. Get function :__ get ()
  41. **************************************** *******/
  42. Function _ get ($ property_name)
  43. {
  44. If (isset ($ this-> $ property_name ))
  45. {
  46. Return ($ this-> $ property_name );
  47. }
  48. Else
  49. {
  50. Return (NULL );
  51. }
  52. }
  53. /*************************************** ******
  54. Set function :__ set ()
  55. **************************************** *******/
  56. Function _ set ($ property_name, $ value)
  57. {
  58. $ This-> $ property_name = $ value;
  59. }
  60. /*************************************** ******
  61. Function name: read_data
  62. Function: reads records from a table based on SQL query statements.
  63. Returned value: two-dimensional attribute array result [record number] [field name]
  64. **************************************** *******/
  65. Function read_data ()
  66. {
  67. $ Psql = $ this-> SQL;
  68. // Query data, database links, and other information should be implemented outside class calls
  69. $ Result = mysql_query ($ psql) or die (mysql_error ());
  70. $ This-> total_records = mysql_num_rows ($ result );
  71. // Use the LIMIT keyword to obtain the record to be displayed on this page
  72. If ($ this-> total_records> 0)
  73. {
  74. $ This-> start_index = ($ this-> current_page-1) * $ this-> page_size;
  75. $ Psql = $ psql. "LIMIT". $ this-> start_index. ",". $ this-> page_size;
  76. $ Result = mysql_query ($ psql) or die (mysql_error ());
  77. $ This-> current_records = mysql_num_rows ($ result );
  78. // Put the query result in the result array
  79. $ I = 0;
  80. While ($ row = mysql_fetch_Array ($ result ))
  81. {
  82. $ This-> result [$ I] = $ row;
  83. $ I ++;
  84. }
  85. }
  86. // Obtain the total number of pages and current page information
  87. $ This-> total_pages = ceil ($ this-> total_records/$ this-> page_size );
  88. $ This-> first = 1;
  89. $ This-> prev = $ this-> current_page-1;
  90. $ This-> next = $ this-> current_page + 1;
  91. $ This-> last = $ this-> total_pages;
  92. }
  93. /*************************************** ******
  94. Function name: standard_navigate ()
  95. Function: displays the home page, next page, last page, and not pages.
  96. **************************************** *******/
  97. Function standard_navigate ()
  98. {
  99. Echo"

    ";

  100. Echo "";
  101. Echo"

    ";
  102. }
  103. /*************************************** ******
  104. Function name: full_navigate ()
  105. Function: displays the home page, next page, last page, and not pages.
  106. Generate navigation links such as 1 2 3... 10 11
  107. **************************************** *******/
  108. Function full_navigate ()
  109. {
  110. Echo"

    ";

  111. Echo "";
  112. Echo"

    ";
  113. }
  114. }
  115. ?>

Paging usage:

  1. Include_once ("fenye_php.php"); // introduces a class
  2. //////////////////////////////////////// ///////////////////////////////
  3. $ Con = mysql_connect ("localhost", "root ","");
  4. If (! $ Con)
  5. {
  6. Die ('could not connect: '. mysql_error ());
  7. }

  8. Mysql_select_db ("myblog", $ con); // select a database

  9. $ PAGE_SIZE = 10; // you can specify the number of entries displayed on each page.
  10. //////////////////////////////////////// ///////////////////////////////
  11. $ PageSupport = new PageSupport ($ PAGE_SIZE); // instantiate the PageSupport object
  12. $ Current_page = $ _ GET ["current_page"]; // The current page number.
  13. If (isset ($ current_page )){
  14. $ PageSupport->__ set ("current_page", $ current_page );
  15. } Else {
  16. $ PageSupport->__ set ("current_page", 1 );
  17. }
  18. $ PageSupport->__ set ("SQL", "select * from article ");
  19. $ PageSupport-> read_data (); // read data
  20. If ($ pageSupport-> current_records> 0) // if the data is not empty, assemble the data
  21. {
  22. For ($ I = 0; $ I <$ pageSupport-> current_records; $ I ++)
  23. {
  24. $ Title = $ pageSupport-> result [$ I] ["title"];
  25. $ Content = $ pageSupport-> result [$ I] ["content"];
  26. $ Part = substr ($ content, 0,400 );
  27. // Output each data recyclically
  28. Echo'

  29. '. $ Title .'

  30. '. $ Part .'

  31. Update delet
  32. ';
  33. }
  34. }
  35. $ PageSupport-> standard_navigate (); // call this function in the class to display the HTML page
  36. // Close the database
  37. Mysql_close ($ con );
  38. ?>

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.