A general method of PHP for data paging

Source: Internet
Author: User
  1. function Show_page ($count, $page, $page _size)
  2. {
  3. $page _count = ceil ($count/$page _size); Calculate the total number of pages
  4. $init = 1;
  5. $page _len=7;
  6. $max _p= $page _count;
  7. $pages = $page _count;
  8. Judging the current page number
  9. $page = (Empty ($page) | | $page <0)? 1: $page;
  10. Gets the current page URL
  11. $url = $_server[' Request_uri ');
  12. Remove the original page parameter from the URL to add a new page parameter
  13. $parsedurl =parse_url ($url);
  14. $url _query = isset ($parsedurl [' query '])? $parsedurl [' query ']: ';
  15. if ($url _query! = ") {
  16. $url _query = Preg_replace ("/(^|&) page= $page/", "", $url _query);
  17. $url = Str_replace ($parsedurl [' query '], $url _query, $url);
  18. if ($url _query! = ") {
  19. $url. = ' & ';
  20. }
  21. } else {
  22. $url. = '? ';
  23. }
  24. Paging function code
  25. $page _len = ($page _len%2)? $page _len: $page _len+1; Number of page number
  26. $pageoffset = ($page _len-1)/2; Left and right offset of page number
  27. $navs = ";
  28. if ($pages! = 0) {
  29. if ($page!=1) {
  30. $navs. = "Home";//First page
  31. $navs. = "Previous page";//Previous Page
  32. } else {
  33. $navs. = " home ";
  34. $navs. = " previous page ";
  35. }
  36. if ($pages > $page _len)
  37. {
  38. If the current page is less than or equal to the left offset
  39. if ($page <= $pageoffset) {
  40. $init = 1;
  41. $max _p = $page _len;
  42. }
  43. else//If the current page is greater than the left offset
  44. {
  45. If the right offset of the current page number exceeds the Max page count
  46. if ($page + $pageoffset >= $pages + 1) {
  47. $init = $pages-$page _len+1;
  48. }
  49. Else
  50. {
  51. Calculations when left and right offsets are present
  52. $init = $page-$pageoffset;
  53. $max _p = $page + $pageoffset;
  54. }
  55. }
  56. }
  57. for ($i = $init; $i <= $max _p; $i + +)
  58. {
  59. if ($i = = $page) {$navs. = "". $i. ';}
  60. else {$navs. = "". $i. "";}
  61. }
  62. if ($page! = $pages)
  63. {
  64. $navs. = "Next page";//Next page
  65. $navs. = "Last";//Last Page
  66. } else {
  67. $navs. = " next page ";
  68. $navs. = " last ";
  69. }
  70. echo "$navs";
  71. }
  72. }
Copy Code
Paging, PHP
  • 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.