PHP Universal pagination Class code, imitation Goolge page style

Source: Internet
Author: User
Tags ereg
  1. /**

  2. * * Generic PHP paging class. (Imitation Google style)
  3. * * Simply provide two parameters for the total number of records and the number of displays per page. (Detailed usage instructions are attached.)
  4. * * No URL is specified and the link is generated by the program. Facilitates the retrieval of results by paging.
  5. * * The form is submitted with the Get method, which guarantees that the URL parameters are not lost when operations such as queries, deletions, and so on.
  6. **/
  7. Class pager{
  8. IE Address Bar address
  9. var $url;
  10. Record total number of bars
  11. var $countall;
  12. Total pages
  13. var $page;
  14. Pagination Digital Link
  15. var $thestr;
  16. Home page, Previous link
  17. var $backstr;
  18. Last page, Next link
  19. var $nextstr;
  20. Current page number
  21. var $pg;
  22. Show number of records per page
  23. var $countlist;
  24. Page Flip Style
  25. var $style;
  26. constructor, which executes the function automatically when instantiating the class
  27. function Pager ($countall, $countlist, $style = "page") {
  28. Number of records and the number of display per page can not be a whole, the page after the remainder plus 1
  29. $this->countall = $countall;
  30. $this->countlist = $countlist;
  31. $this->style= $style;
  32. if ($this->countall% $this->countlist!=0) {
  33. $this->page=sprintf ("%d", $this->countall/$this->countlist) +1;
  34. }else{
  35. $this->page= $this->countall/$this->countlist;
  36. }

  37. $this->pg=$_get["PG"];

  38. Ensure that the PG starts from page 1th without specifying the case
  39. if (!ereg ("^[1-9][0-9]*$", $this->pg) | | empty ($this->PG)) {
  40. $this->pg=1;
  41. }
  42. The page number exceeds the maximum range and takes the maximum value
  43. if ($this->pg> $this->page) {
  44. $this->pg= $this->page;
  45. }
  46. Gets the current URL. See the bottom of the function entity for specific implementations
  47. $this->url = Pager::geturl ();
  48. Replace the wrong format with the correct page number
  49. if (Isset ($_get["PG"]) && $_get["PG"]!= $this->PG) {
  50. $this->url=str_replace ("? pg=". $_get["PG"], "? pg= $this->pg", $this->url);
  51. $this->url=str_replace ("&pg=". $_get["PG"], "&pg= $this->pg", $this->url);
  52. }
  53. Generates pagination in digital form 12345.
  54. if ($this->page<=10) {
  55. for ($i =1; $i < $this->page+1; $i +) {
  56. $this->thestr= $this->thestr. PAGER::MAKEPG ($i, $this->PG);
  57. }
  58. }else{
  59. if ($this->pg<=5) {
  60. for ($i =1; $i <10; $i + +) {
  61. $this->thestr= $this->thestr. PAGER::MAKEPG ($i, $this->PG);
  62. }
  63. }else{
  64. if (6+ $this->pg<= $this->page) {
  65. for ($i = $this->pg-4; $i < $this->pg+6; $i + +) {
  66. $this->thestr= $this->thestr. PAGER::MAKEPG ($i, $this->PG);
  67. }
  68. }else{
  69. for ($i = $this->pg-4; $i < $this->page+1; $i + +) {
  70. $this->thestr= $this->thestr. PAGER::MAKEPG ($i, $this->PG);
  71. }

  72. }

  73. }
  74. }
  75. Generate text links on the next page of the previous page
  76. $this->backstr = pager::gotoback ($this->pg);
  77. $this->nextstr = Pager::gotonext ($this->pg, $this->page);
  78. Echo ("Total". $this->countall. "Bar, per page". $this->countlist. " Bar, Total ". $this->page." Page ". $this->backstr. $this->thestr. $this->nextstr);
  79. }
  80. Auxiliary functions to generate digital paging
  81. function Makepg ($i, $PG) {
  82. if ($i = = $PG) {
  83. return "style." > ". $i.";
  84. }else{
  85. Return "url,5, $i)." class= ". $this->style." >". $i.";
  86. }
  87. }
  88. A function that generates information such as the previous page
  89. function Gotoback ($PG) {
  90. if ($pg -1>0) {
  91. return $this->gotoback= "url,3,0". "class=". $this->style. "' > Home url,2,0). "class=". $this->style. "' > Previous page ";
  92. }else{
  93. return $this->gotoback= " first prev";
  94. }
  95. }
  96. A function to generate information such as the next page
  97. function GoToNext ($PG, $page) {
  98. if ($pg < $page) {
  99. Return "url,1,0". "class=". $this->style. " > next page url,4,0). "class=". $this->style. "' > Last ";
  100. }else{
  101. Return " next page last ";
  102. }
  103. } bbs.it-home.org
  104. Handles $PG methods in URLs for automatic generation of pg=x
  105. function Replacepg ($url, $flag, $i) {
  106. if ($flag = = 1) {
  107. $temp _PG = $this->pg;
  108. Return Str_replace ("pg=". $temp _pg, "pg=". $this->pg+1), $url);
  109. }else if ($flag = = 2) {
  110. $temp _PG = $this->pg;
  111. Return Str_replace ("pg=". $temp _pg, "pg=". $this->pg-1), $url);
  112. }else if ($flag = = 3) {
  113. $temp _PG = $this->pg;
  114. Return Str_replace ("pg=". $temp _pg, "pg=1", $url);
  115. }else if ($flag = = 4) {
  116. $temp _PG = $this->pg;
  117. Return Str_replace ("pg=". $temp _pg, "pg=". $this->page, $url);
  118. }else if ($flag = = 5) {
  119. $temp _PG = $this->pg;
  120. Return Str_replace ("pg=". $temp _pg, "pg=". $i, $url);
  121. }else{
  122. return $url;
  123. }
  124. }
  125. How to get the current URL
  126. function GetUrl () {
  127. $url = "http://". $_server["Http_host"];
  128. if (Isset ($_server["Request_uri")) {
  129. $url. =$_server["Request_uri"];
  130. }else{
  131. $url. =$_server["Php_self"];
  132. if (!empty ($_server["query_string")) {
  133. $url. = "?". $_server["Query_string"];
  134. }
  135. }
  136. Add the Pg=x Word to the current URL
  137. if (!ereg ("(pg=| pg=|pg=| pg=) ", $url)) {
  138. if (!strpos ($url, "?")) {
  139. $url = $url. "? Pg=1 ";
  140. }else{
  141. $url = $url. " &pg=1 ";
  142. }
  143. }
  144. return $url;
  145. }
  146. }
  147. ?>

Copy Code
  • 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.