Example of function for generating page-turning link list in PHP
Release: 2016-07-25 08:57:51
Original
951 people have browsed it
-
- /**
- * Generate page number list
- * @param int $element_total_count Total number of elements
- * @param int $current_page Current page
- * @param int $per_page_elem_count Number of elements per page
- * @param int $show_page_num Number of page numbers displayed in the list
- * @param string $up_down_class Up and down page turning style
- * @param string $num_class current page number style
- * @param string $href page link
- * @param string $page_symbol link parameter passing the page number
- * @return string
- * @site bbs.it-home.org
- */
- public static function getPageListLink($element_total_count,$current_page=1,$per_page_elem_count=10,
- $show_page_num=10,$up_down_class='',$num_class='',$href='',$page_symbol='p')
- {
- if(empty($href)) {
- //自动取得剔除页码参数的页面链接
- $page_name = basename($_SERVER['PHP_SELF']);
- $params = $_SERVER['QUERY_STRING'];
- $params_str = '';
- if(!empty($params)) {
- $params = str_replace('&', '&', $params);
- $params_array = explode('&', $params);
- foreach($params_array as $param) {
- if(!empty($param)) {
- $index = strpos($param, '=');
- if($index) {
- $key = substr($param, 0, $index);
- if($key && $key != $page_symbol)
- $params_str .= $param . '&';
- }
- }
- }
- }
- if(!empty($params_str))
- $href = $page_name . '?' . $params_str;
- else
- $href = $page_name;
- $href = rtrim($href,'&');
- }
- $prefix = strpos($href,"?") ? "&" : "?";
- $prefix .= $page_symbol;
- $page_total_count = ceil($element_total_count/$per_page_elem_count);
- if(intval($element_total_count)< 1 || !isset($element_total_count)) {
- return '';
- }
- if($element_total_count <= $per_page_elem_count)
- return '';
- if($current_page>$page_total_count)
- $current_page = 1;
- if(strpos($href,"#")) {
- $label = substr($href,strpos($href,"#"));
- $href = substr($href,0,strpos($href,"#"));
- }
- /* 生成页码 */
- if($current_page > ceil($show_page_num/2)) {
- $start = $current_page - ceil($show_page_num/2);
- $end = (($current_page+ceil($show_page_num/2))<$page_total_count) ?
- $current_page+ceil($show_page_num/2)-1 : $page_total_count;
- } else {
- $start = 1;
- $end = ($show_page_num>$page_total_count) ? $page_total_count : $show_page_num;
- }
- if(!empty($num_class))
- $num_class_str = ' class="'.$num_class.'"';
- else
- $num_class_str = '';
- $page_num_string = '';
- for($i=$start;$i<=$end;$i++) {
- if(intval($i) == intval($current_page))
- $page_num_string .= ''.$i.'';
- else
- $page_num_string .= ''.$i.'';
- }
- /* 上下翻页 */
- $prev_page = (intval($current_page-1)>0)?intval($current_page-1):0;
- $next_page = (intval($current_page)<$page_total_count) ? intval($current_page+1) : 0;
- if(!empty($up_down_class))
- $up_down_class_str = ' class="'.$up_down_class.'"';
- else
- $up_down_class_str = '';
- $page_up_string = '';
- if(intval($prev_page) > 0)
- $page_up_string = '上一页';
- else
- $page_up_string = '上一页';
- $page_down_string = '';
- if(intval($next_page) > 0)
- $page_down_string .= '下一页';
- else
- $page_down_string .= '下一页';
- return $page_up_string . $page_num_string . $page_down_string;
- }
-
复制代码
|
Statement of this Website
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31