首頁  >  文章  >  後端開發  >  php產生翻頁連結清單的函數舉例

php產生翻頁連結清單的函數舉例

WBOY
WBOY原創
2016-07-25 08:57:51903瀏覽
  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)< 1 || !isset($element_total_count)) {
  46. return '';
  47. }
  48. if($element_total_count <= $per_page_elem_count)
  49. return '';
  50. if($current_page>$page_total_count)
  51. $current_page = 1;
  52. if(strpos($href,"#")) {
  53. $label = substr($href,strpos($href,"#"));
  54. $href = substr($href,0,strpos($href,"#"));
  55. }
  56. /* 生成页码 */
  57. if($current_page > ceil($show_page_num/2)) {
  58. $start = $current_page - ceil($show_page_num/2);
  59. $end = (($current_page+ceil($show_page_num/2))<$page_total_count) ?
  60. $current_page+ceil($show_page_num/2)-1 : $page_total_count;
  61. } else {
  62. $start = 1;
  63. $end = ($show_page_num>$page_total_count) ? $page_total_count : $show_page_num;
  64. }
  65. if(!empty($num_class))
  66. $num_class_str = ' class="'.$num_class.'"';
  67. else
  68. $num_class_str = '';
  69. $page_num_string = '';
  70. for($i=$start;$i<=$end;$i++) {
  71. if(intval($i) == intval($current_page))
  72. $page_num_string .= ''.$i.'';
  73. else
  74. $page_num_string .= ''.$i.'';
  75. }
  76. /* 上下翻页 */
  77. $prev_page = (intval($current_page-1)>0)?intval($current_page-1):0;
  78. $next_page = (intval($current_page)<$page_total_count) ? intval($current_page+1) : 0;
  79. if(!empty($up_down_class))
  80. $up_down_class_str = ' class="'.$up_down_class.'"';
  81. else
  82. $up_down_class_str = '';
  83. $page_up_string = '';
  84. if(intval($prev_page) > 0)
  85. $page_up_string = '上一页';
  86. else
  87. $page_up_string = '上一页';
  88. $page_down_string = '';
  89. if(intval($next_page) > 0)
  90. $page_down_string .= '下一页';
  91. else
  92. $page_down_string .= '下一页';
  93. return $page_up_string . $page_num_string . $page_down_string;
  94. }
复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn