A simple generic paging class written by myself-php Tutorial

Source: Internet
Author: User
A simple generic paging class written by a user
Simple paging class written by php for personal learning, with strong versatility

  1. /**
  2. * Simple paging class
  3. * @ Author: phpprince
  4. * @ QQ: 8923052.
  5. * @ Date: 21:08:35
  6. */
  7. Class Page {
  8. Protected $ url; // address
  9. Protected $ allnum; // total number of records
  10. Protected $ current; // current page number
  11. Protected $ pagesize; // number of records displayed on each page
  12. Protected $ postfix; // suffix
  13. Protected $ style; // A total of three display styles. 1 2 3 represents the first 5, the last 4, the last 3, and the last 4, respectively.
  14. Public function _ construct ($ url, $ allnum, $ current, $ pagesize = 10, $ postfix = '', $ style = 1 ){
  15. $ This-> url = $ url;
  16. $ This-> allnum = $ allnum;
  17. $ This-> current = $ current;
  18. $ This-> pagesize = $ pagesize;
  19. $ This-> postfix = $ postfix;
  20. $ This-> style = $ style;
  21. }
  22. // Obtain the total number of pages
  23. Protected function maxPageNum (){
  24. $ Max = ceil ($ this-> allnum/$ this-> pagesize );
  25. // Page number exceeding correction
  26. If ($ this-> current> $ max ){
  27. $ This-> current = $ max;
  28. }
  29. If ($ this-> current <1 ){
  30. $ This-> current = 1;
  31. }
  32. Return $ max;
  33. }
  34. // Obtain the complete html str link on the first page
  35. Protected function firstUrl (){
  36. If ($ this-> current! = 1)
  37. {
  38. Return 'geturl (1). '"title =" View first Page "> homepage ';
  39. } Else {
  40. Return 'homepage ';
  41. }
  42. }
  43. // Obtain the complete html str link of the previous page
  44. Protected function prevUrl (){
  45. If ($ this-> current <= 1 ){
  46. $ Fullurl = 'preview ';
  47. } Else {
  48. $ Fullurl = $ this-> url. ($ this-> current-1). $ this-> postfix;
  49. $ Fullurl = 'preview ';
  50. }
  51. Return $ fullurl;
  52. }
  53. // Obtain the link of the next page. complete html str
  54. Protected function nextUrl (){
  55. If ($ this-> current >=$ this-> maxPageNum ()){
  56. $ Fullurl = 'next page ';
  57. } Else {
  58. $ Fullurl = $ this-> url. ($ this-> current + 1). $ this-> postfix;
  59. $ Fullurl = 'next page ';
  60. }
  61. Return $ fullurl;
  62. }
  63. // Obtain the complete html str link on the last page
  64. Protected function lastUrl (){
  65. If ($ this-> current >=$ this-> maxPageNum ()){
  66. $ Fullurl = 'Last page ';
  67. } Else {
  68. $ Fullurl = $ this-> url. $ this-> maxPageNum (). $ this-> postfix;
  69. $ Fullurl = 'Last page ';
  70. }
  71. Return $ fullurl;
  72. }
  73. // Obtain the complete url of the specified page number
  74. Protected function getUrl ($ pageno ){
  75. Return $ this-> url. $ pageno. $ this-> postfix;
  76. }
  77. // Specify the display style
  78. Protected function getStyle (){
  79. Switch ($ this-> style ){
  80. Case 1:
  81. $ Before = 5;
  82. $ After = 4;
  83. Break;
  84. Case 2:
  85. $ Before = 3;
  86. $ After = 3;
  87. Break;
  88. Case 3:
  89. $ Before = 4;
  90. $ After = 4;
  91. Break;
  92. Default:
  93. $ Before = 5;
  94. $ After = 4;
  95. }
  96. Return array ($ before, $ after );
  97. }
  98. // Obtain the intermediate URL 1 2 3 4 5 6 7 8 9 10
  99. Protected function getMiddelUrl (){
  100. $ Max = $ this-> maxPageNum (); // obtain the total page first to check whether the current page has exceeded the limit.
  101. $ Current = $ this-> current; // The current page number must be valid to ensure that the following page number ranges are correct.
  102. // Obtain the current style
  103. List ($ before, $ after) = $ this-> getStyle ();
  104. $ Startno = $ current-$ before; // start page number
  105. $ Endno = $ current + $ after; // end page number
  106. // To ensure that the output always meets the requirements, when the page is not exceeded, the start page must be added, and vice versa.
  107. While ($ endno> $ max | $ startno <1 ){
  108. If ($ endno> $ max) {// ends the page number exceeding the limit
  109. $ Endno --;
  110. If ($ startno> 1 ){
  111. $ Startno --;
  112. }
  113. }
  114. If ($ startno <1) {// the starting page number is out of bounds.
  115. $ Startno ++;
  116. If ($ endno <$ max ){
  117. $ Endno ++;
  118. }
  119. }
  120. }
  121. $ Str = ''; // used to save the entire html str
  122. For ($ I = $ startno; $ I <= $ endno; $ I ++ ){
  123. $ Currenturl = $ this-> getUrl ($ I );
  124. If ($ I! = $ Current ){
  125. $ Str. = "{$ I }";
  126. } Else {
  127. $ Str. = ''. $ I .'';
  128. }
  129. }
  130. Return $ str;
  131. }
  132. // Returns the complete pagination html string
  133. Public function getPageStr (){
  134. $ Str ='

    '. $ This-> firstUrl (). $ this-> prevUrl ();

  135. $ Str. = $ this-> getMiddelUrl ();
  136. $ Str. = $ this-> nextUrl (). $ this-> lastUrl (). 'col '. $ this-> maxPageNum (). 'page '. $ this-> allnum. 'entries

    ';
  137. Return $ str;
  138. }
  139. }
  140. ?>

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.