Home  >  Article  >  Backend Development  >  Example of function for generating page-turning link list in PHP

Example of function for generating page-turning link list in PHP

WBOY
WBOYOriginal
2016-07-25 08:57:51950browse
  1. /**
  2. * Generate page number list
  3. * @param int $element_total_count Total number of elements
  4. * @param int $current_page Current page
  5. * @param int $per_page_elem_count Number of elements per page
  6. * @param int $show_page_num Number of page numbers displayed in the list
  7. * @param string $up_down_class Up and down page turning style
  8. * @param string $num_class current page number style
  9. * @param string $href page link
  10. * @param string $page_symbol link parameter passing the page number
  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. }
复制代码


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