欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 生成翻页链接(页码)列表的函数

程序员文章站 2022-04-22 12:20:44
...
  1. /**
  2. * 生成页码列表
  3. *
  4. * @param int $element_total_count 元素总数
  5. * @param int $current_page 当前页
  6. * @param int $per_page_elem_count 每页元素数
  7. * @param int $show_page_num 列表显示的页码数
  8. * @param string $up_down_class 上下翻页样式
  9. * @param string $num_class 当前页页码数字样式
  10. * @param string $href 页面链接
  11. * @param string $page_symbol 传递页码数的链接参数
  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) {
  50. return '';
  51. }
  52. if($element_total_count return '';
  53. if($current_page>$page_total_count)
  54. $current_page = 1;
  55. if(strpos($href,"#"))
  56. {
  57. $label = substr($href,strpos($href,"#"));
  58. $href = substr($href,0,strpos($href,"#"));
  59. }
  60. /* 生成页码 */
  61. if($current_page > ceil($show_page_num/2))
  62. {
  63. $start = $current_page - ceil($show_page_num/2);
  64. $end = (($current_page+ceil($show_page_num/2)) $current_page+ceil($show_page_num/2)-1 : $page_total_count;
  65. }
  66. else
  67. {
  68. $start = 1;
  69. $end = ($show_page_num>$page_total_count) ? $page_total_count : $show_page_num;
  70. }
  71. if(!empty($num_class))
  72. $num_class_str = ' class="'.$num_class.'"';
  73. else
  74. $num_class_str = '';
  75. $page_num_string = '';
  76. for($i=$start;$i {
  77. if(intval($i) == intval($current_page))
  78. $page_num_string .= ''.$i.'';
  79. else
  80. $page_num_string .= ''.$i.'';
  81. }
  82. /* 上下翻页 */
  83. $prev_page = (intval($current_page-1)>0)?intval($current_page-1):0;
  84. $next_page = (intval($current_page) if(!empty($up_down_class))
  85. $up_down_class_str = ' class="'.$up_down_class.'"';
  86. else
  87. $up_down_class_str = '';
  88. $page_up_string = '';
  89. if(intval($prev_page) > 0)
  90. $page_up_string = '上一页';
  91. else
  92. $page_up_string = '上一页';
  93. $page_down_string = '';
  94. if(intval($next_page) > 0)
  95. $page_down_string .= '下一页';
  96. else
  97. $page_down_string .= '下一页';
  98. /* 返回结果 */
  99. return $page_up_string . $page_num_string . $page_down_string;
  100. }
  101. ?>
复制代码