PHP generate page link (page number) List of functions

Source: Internet
Author: User
  1. /**
  2. * Generate page List
  3. *
  4. * @param int $element Total number of _total_count elements
  5. * @param int $current _page Current page
  6. * @param int $per _page_elem_count per page element
  7. * Number of pages displayed @param int $show _page_num list
  8. * @param string $up _down_class page up and down style
  9. * @param string $num _class current page number style
  10. * @param string $href page link
  11. * @param string $page _symbol The link parameter of the number of pages passed
  12. * @return String
  13. */
  14. function Get_page_link_list ($element _total_count, $current _page=1, $per _page_elem_count=10, $show _page_num=10, $up _ Down_class, $num _class, $href, $page _symbol= ' P ')
  15. {
  16. if (empty ($href))
  17. {
  18. Automatically get page links that exclude page number parameters
  19. $page _name = basename ($_server[' php_self ');
  20. $params = $_server[' query_string ');
  21. $params _str = ";
  22. if (!empty ($params))
  23. {
  24. $params = Str_replace (' & ', ' & ', $params);
  25. $params _array = Explode (' & ', $params);
  26. foreach ($params _array as $param)
  27. {
  28. if (!empty ($param))
  29. {
  30. $index = Strpos ($param, ' = ');
  31. if ($index)
  32. {
  33. $key = substr ($param, 0, $index);
  34. if ($key && $key! = $page _symbol)
  35. $params _str. = $param. ' & ';
  36. }
  37. }
  38. }
  39. }
  40. if (!empty ($params _str))
  41. $href = $page _name. '?' . $params _str;
  42. Else
  43. $href = $page _name;
  44. $href = RTrim ($href, ' & ');
  45. }
  46. $prefix = Strpos ($href, "?")? " & ":"? ";
  47. $prefix. = $page _symbol;
  48. $page _total_count = ceil ($element _total_count/$per _page_elem_count);
  49. if (Intval ($element _total_count) < 1 | |!isset ($element _total_count))
  50. {
  51. Return ';
  52. }
  53. if ($element _total_count <= $per _page_elem_count)
  54. Return ';
  55. if ($current _page> $page _total_count)
  56. $current _page = 1;
  57. if (Strpos ($href, "#"))
  58. {
  59. $label = substr ($href, Strpos ($href, "#"));
  60. $href = substr ($href, 0,strpos ($href, "#"));
  61. }
  62. /* Generate page Number */
  63. if ($current _page > Ceil ($show _page_num/2))
  64. {
  65. $start = $current _page-ceil ($show _PAGE_NUM/2);
  66. $end = (($current _page+ceil ($show _page_num/2)) < $page _total_count)?
  67. $current _page+ceil ($show _page_num/2)-1: $page _total_count;
  68. }
  69. Else
  70. {
  71. $start = 1;
  72. $end = ($show _page_num> $page _total_count)? $page _total_count: $show _page_num;
  73. }
  74. if (!empty ($num _class))
  75. $num _class_str = ' class= '. $num _class. ' ";
  76. Else
  77. $num _class_str = ";
  78. $page _num_string = ";
  79. for ($i = $start; $i <= $end; $i + +)
  80. {
  81. if (intval ($i) = = Intval ($current _page))
  82. $page _num_string. = '. $i. ';
  83. Else
  84. $page _num_string. = '. $i. ';
  85. }
  86. /* PAGE UP/down *
  87. $prev _page = (intval ($current _page-1) >0)? Intval ($current _page-1): 0;
  88. $next _page = (intval ($current _page) < $page _total_count)? Intval ($current _page+1): 0;
  89. if (!empty ($up _down_class))
  90. $up _down_class_str = ' class= '. $up _down_class. ' ";
  91. Else
  92. $up _down_class_str = ";
  93. $page _up_string = ";
  94. if (Intval ($prev _page) > 0)
  95. $page _up_string = ' previous page ';
  96. Else
  97. $page _up_string = ' previous page ';
  98. $page _down_string = ";
  99. if (Intval ($next _page) > 0)
  100. $page _down_string. = ' next page ';
  101. Else
  102. $page _down_string. = ' next page ';
  103. /* Return results */
  104. Return $page _up_string. $page _num_string. $page _down_string;
  105. }
  106. ?>
Copy Code
  • Related Article

    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.