Nice php + mysql paging class

Source: Internet
Author: User
Nice php + mysql paging class

  1. /*****************************
  2. * @ Class name: page
  3. * @ Parameter: $ myde_total-total number of records
  4. * $ Myde_size-number of records displayed on one page
  5. * $ Myde_page-current page
  6. * $ Myde_url-get the current url
  7. * @ Function: Paging implementation
  8. * @ Author: Song Haige
  9. */
  10. Class page {
  11. Private $ myde_total; // total number of records
  12. Private $ myde_size; // number of records displayed on one page
  13. Private $ myde_page; // current page
  14. Private $ myde_page_count; // total number of pages
  15. Private $ myde_ I; // Start Page
  16. Private $ myde_en; // The ending page number.
  17. Private $ myde_url; // Obtain the current url
  18. /*
  19. * $ Show_pages
  20. * Page display format. the page number of the link displayed is 2 * $ show_pages + 1.
  21. * For example, $ show_pages = 2, the page is displayed on the [Homepage] [Previous Page] 1 2 3 4 5 [Next Page] [last page]
  22. */
  23. Private $ show_pages;
  24. Public function _ construct ($ myde_total = 1, $ myde_size = 1, $ myde_page = 1, $ myde_url, $ show_pages = 2 ){
  25. $ This-> myde_total = $ this-> numeric ($ myde_total );
  26. $ This-> myde_size = $ this-> numeric ($ myde_size );
  27. $ This-> myde_page = $ this-> numeric ($ myde_page );
  28. $ This-> myde_page_count = ceil ($ this-> myde_total/$ this-> myde_size );
  29. $ This-> myde_url = $ myde_url;
  30. If ($ this-> myde_total <0) $ this-> myde_total = 0;
  31. If ($ this-> myde_page <1) $ this-> myde_page = 1;
  32. If ($ this-> myde_page_count <1) $ this-> myde_page_count = 1;
  33. If ($ this-> myde_page> $ this-> myde_page_count) $ this-> myde_page = $ this-> myde_page_count;
  34. $ This-> limit = ($ this-> myde_page-1) * $ this-> myde_size;
  35. $ This-> myde_ I = $ this-> myde_page-$ show_pages;
  36. $ This-> myde_en = $ this-> myde_page + $ show_pages;
  37. If ($ this-> myde_ I <1 ){
  38. $ This-> myde_en = $ this-> myde_en + (1-$ this-> myde_ I );
  39. $ This-> myde_ I = 1;
  40. }
  41. If ($ this-> myde_en> $ this-> myde_page_count ){
  42. $ This-> myde_ I = $ this-> myde_ I-($ this-> myde_en-$ this-> myde_page_count );
  43. $ This-> myde_en = $ this-> myde_page_count;
  44. }
  45. If ($ this-> myde_ I <1) $ this-> myde_ I = 1;
  46. }
  47. // Check whether it is a number
  48. Private function numeric ($ num ){
  49. If (strlen ($ num )){
  50. If (! Preg_match ("/^ [0-9] + $/", $ num )){
  51. $ Num = 1;
  52. } Else {
  53. $ Num = substr ($ num, 0, 11 );
  54. }
  55. } Else {
  56. $ Num = 1;
  57. }
  58. Return $ num;
  59. }
  60. // Address replacement
  61. Private function page_replace ($ page ){
  62. Return str_replace ("{page}", $ page, $ this-> myde_url );
  63. }
  64. // Homepage
  65. Private function myde_home (){
  66. If ($ this-> myde_page! = 1 ){
  67. Return "page_replace (1)." "title =" homepage "> homepage ";
  68. } Else {
  69. Return"

    Homepage

    ";
  70. }
  71. }
  72. // Previous Page
  73. Private function myde_prev (){
  74. If ($ this-> myde_page! = 1 ){
  75. Return "page_replace ($ this-> myde_page-1)." "title =" previous "> previous ";
  76. } Else {
  77. Return"

    Previous Page

    ";
  78. }
  79. }
  80. // Next page
  81. Private function myde_next (){
  82. If ($ this-> myde_page! = $ This-> myde_page_count ){
  83. Return "page_replace ($ this-> myde_page + 1)." "title =" Next Page "> Next Page ";
  84. } Else {
  85. Return"

    Next page

    ";
  86. }
  87. }
  88. // Last Page
  89. Private function myde_last (){
  90. If ($ this-> myde_page! = $ This-> myde_page_count ){
  91. Return "page_replace ($ this-> myde_page_count)." "title =" Last Page "> last page ";
  92. } Else {
  93. Return"

    Last page

    ";
  94. }
  95. }
  96. // Output
  97. Public function myde_write ($ id = 'page '){
  98. $ Str ="

    ";

  99. $ Str. = $ this-> myde_home ();
  100. $ Str. = $ this-> myde_prev ();
  101. If ($ this-> myde_ I> 1 ){
  102. $ Str. ="

    ...

    ";
  103. }
  104. For ($ I = $ this-> myde_ I; $ I <= $ this-> myde_en; $ I ++ ){
  105. If ($ I ==$ this-> myde_page ){
  106. $ Str. = "page_replace ($ I)." "title =" no. ". $ I." page "class =" cur "> $ I ";
  107. } Else {
  108. $ Str. = "page_replace ($ I)." "title =" no. ". $ I." Page "> $ I ";
  109. }
  110. }
  111. If ($ this-> myde_en <$ this-> myde_page_count ){
  112. $ Str. ="

    ...

    ";
  113. }
  114. $ Str. = $ this-> myde_next ();
  115. $ Str. = $ this-> myde_last ();
  116. $ Str. ="

    Total". $ This-> myde_page_count.

  117. "Page". $ This-> myde_total ."Data entries

    ";
  118. $ Str. ="

    ";
  119. Return $ str;
  120. }
  121. }
  122. ?>

Php + mysql paging principle: limit ($ curpage-1) * $ showrow, $ showrow

2. php display page code

  1. Require_once ('./page. class. php'); // paging class
  2. $ Showrow = 3; // number of lines displayed on one page
  3. $ Curpage = empty ($ _ GET ['Page'])? 1: $ _ GET ['Page']; // The current page should also handle non-numbers.
  4. $ Url = "? Page = {page} "; // page address. if the search condition is "? Page = {page} & q = ". $ _ GET ['Q']
  5. // The code for connecting to mysql is omitted. you can add the code for testing.
  6. $ SQL = "SELECT * FROM table ";
  7. $ Query = mysql_query ($ SQL );
  8. $ Total = mysql_num_rows ($ query); // The total number of records
  9. If (! Empty ($ _ GET ['Page']) & $ total! = 0 & $ curpage> ceil ($ total/$ showrow ))
  10. $ Curpage = ceil ($ total_rows/$ showrow); // The current page number is greater than the last page number.
  11. // Obtain data
  12. $ Get_data = "select * from table limit". ($ curpage-1) * $ showrow. ", $ showrow ;";
  13. ...
  14. ?>
  15. Simple and common PHP paging class -bbs.it-home.org
  16. If ($ total> $ showrow) {// The total number of records is greater than the number displayed on each page.
  17. $ Page = new page ($ total, $ showrow, $ curpage, $ url, 2 );
  18. Echo $ page-> myde_write ();
  19. }
  20. ?>

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.