Home >Backend Development >PHP Tutorial >PHP function to generate a list of page turning links (page numbers)

PHP function to generate a list of page turning links (page numbers)

WBOY
WBOYOriginal
2016-07-25 09:00:371065browse
  1. /**
  2. * Generate page number list
  3. *
  4. * @param int $element_total_count Total number of elements
  5. * @param int $current_page Current page
  6. * @param int $per_page_elem_count Number of elements per page
  7. * @param int $show_page_num Number of page numbers displayed in the list
  8. * @param string $up_down_class Page up and down style
  9. * @param string $num_class Number style of the current page number
  10. * @param string $href Page link
  11. * @param string $page_symbol Link parameter passing the page number
  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. //自动取得剔除页码参数的页面链接
  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. /* 生成页码 */
  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. /* 上下翻页 */
  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 = '上一页';
  96. else
  97. $page_up_string = '上一页';
  98. $page_down_string = '';
  99. if(intval($next_page) > 0)
  100. $page_down_string .= '下一页';
  101. else
  102. $page_down_string .= '下一页';
  103. /* 返回结果 */
  104. return $page_up_string . $page_num_string . $page_down_string;
  105. }
  106. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn