当前位置:Gxlcms > PHP教程 > php生成翻页链接(页码)列表的函数

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

时间:2021-07-01 10:21:17 帮助过:13人阅读

  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)< 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. ?>

人气教程排行