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

php生成翻页链接列表的函数举例

程序员文章站 2022-04-23 19:33:50
...
  1. /**
  2. * 生成页码列表
  3. * @param int $element_total_count 元素总数
  4. * @param int $current_page 当前页
  5. * @param int $per_page_elem_count 每页元素数
  6. * @param int $show_page_num 列表显示的页码数
  7. * @param string $up_down_class 上下翻页样式
  8. * @param string $num_class 当前页页码数字样式
  9. * @param string $href 页面链接
  10. * @param string $page_symbol 传递页码数的链接参数
  11. * @return string
  12. * @site bbs.it-home.org
  13. */
  14. public static function getPageListLink($element_total_count,$current_page=1,$per_page_elem_count=10,
  15. $show_page_num=10,$up_down_class='',$num_class='',$href='',$page_symbol='p')
  16. {
  17. if(empty($href)) {
  18. //自动取得剔除页码参数的页面链接
  19. $page_name = basename($_SERVER['PHP_SELF']);
  20. $params = $_SERVER['QUERY_STRING'];
  21. $params_str = '';
  22. if(!empty($params)) {
  23. $params = str_replace('&', '&', $params);
  24. $params_array = explode('&', $params);
  25. foreach($params_array as $param) {
  26. if(!empty($param)) {
  27. $index = strpos($param, '=');
  28. if($index) {
  29. $key = substr($param, 0, $index);
  30. if($key && $key != $page_symbol)
  31. $params_str .= $param . '&';
  32. }
  33. }
  34. }
  35. }
  36. if(!empty($params_str))
  37. $href = $page_name . '?' . $params_str;
  38. else
  39. $href = $page_name;
  40. $href = rtrim($href,'&');
  41. }
  42. $prefix = strpos($href,"?") ? "&" : "?";
  43. $prefix .= $page_symbol;
  44. $page_total_count = ceil($element_total_count/$per_page_elem_count);
  45. if(intval($element_total_count) return '';
  46. }
  47. if($element_total_count return '';
  48. if($current_page>$page_total_count)
  49. $current_page = 1;
  50. if(strpos($href,"#")) {
  51. $label = substr($href,strpos($href,"#"));
  52. $href = substr($href,0,strpos($href,"#"));
  53. }
  54. /* 生成页码 */
  55. if($current_page > ceil($show_page_num/2)) {
  56. $start = $current_page - ceil($show_page_num/2);
  57. $end = (($current_page+ceil($show_page_num/2)) $current_page+ceil($show_page_num/2)-1 : $page_total_count;
  58. } else {
  59. $start = 1;
  60. $end = ($show_page_num>$page_total_count) ? $page_total_count : $show_page_num;
  61. }
  62. if(!empty($num_class))
  63. $num_class_str = ' class="'.$num_class.'"';
  64. else
  65. $num_class_str = '';
  66. $page_num_string = '';
  67. for($i=$start;$i if(intval($i) == intval($current_page))
  68. $page_num_string .= ''.$i.'';
  69. else
  70. $page_num_string .= ''.$i.'';
  71. }
  72. /* 上下翻页 */
  73. $prev_page = (intval($current_page-1)>0)?intval($current_page-1):0;
  74. $next_page = (intval($current_page) if(!empty($up_down_class))
  75. $up_down_class_str = ' class="'.$up_down_class.'"';
  76. else
  77. $up_down_class_str = '';
  78. $page_up_string = '';
  79. if(intval($prev_page) > 0)
  80. $page_up_string = '上一页';
  81. else
  82. $page_up_string = '上一页';
  83. $page_down_string = '';
  84. if(intval($next_page) > 0)
  85. $page_down_string .= '下一页';
  86. else
  87. $page_down_string .= '下一页';
  88. return $page_up_string . $page_num_string . $page_down_string;
  89. }
复制代码