Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1180358
  • 博文数量: 252
  • 博客积分: 5421
  • 博客等级: 大校
  • 技术积分: 2418
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-17 12:59
文章分类

全部博文(252)

文章存档

2017年(3)

2016年(18)

2015年(31)

2014年(18)

2013年(7)

2012年(8)

2011年(12)

2010年(30)

2009年(32)

2008年(57)

2007年(36)

分类: Web开发

2008-07-20 16:22:41


点击(此处)折叠或打开

  1. <?php
  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_class 向上翻页样式
  9.  * @param string $up_text 向上翻页文本
  10.  * @param string $down_class 向下翻页样式
  11.  * @param string $down_text 向下翻页文本
  12.  * @param string $curr_num_class 当前页页码数字样式
  13.  * @param string $href 页面链接
  14.  * @param string $page_symbol 传递页码数的链接参数
  15.  * @param string $a_container_pre a标签外围容器的开始标签
  16.  * @param string $a_container_suf a标签外围容器的结束标签
  17.  *
  18.  * @return string
  19.  */
  20. function getPageListLink($element_total_count, $current_page=1, $per_page_elem_count=10,
  21.         $show_page_num=10, $up_class='', $up_text='上一页', $down_class='', $down_text='下一页', $curr_num_class='',
  22.         $href='', $page_symbol='p', $a_container_pre='', $a_container_suf='')
  23. {
  24.     if(empty($href)) {
  25.         //自动取得剔除页码参数的页面链接
  26.         $page_name = basename($_SERVER['PHP_SELF']);
  27.         $params = $_SERVER['QUERY_STRING'];
  28.         $params_str = '';
  29.         if(!empty($params)) {
  30.             $params = str_replace('&', '&', $params);
  31.             $params_array = explode('&', $params);
  32.             foreach($params_array as $param) {
  33.                 if(!empty($param)) {
  34.                     $index = strpos($param, '=');
  35.                     if($index) {
  36.                         $key = substr($param, 0, $index);
  37.                         if($key && $key != $page_symbol)
  38.                             $params_str .= $param . '&';
  39.                     }
  40.                 }
  41.             }
  42.         }
  43.         if(!empty($params_str))
  44.             $href = $page_name . '?' . $params_str;
  45.         else
  46.             $href = $page_name;
  47.         $href = rtrim($href,'&');
  48.     }
  49.     $prefix = strpos($href,"?") ? "&" : "?";
  50.     $prefix .= $page_symbol;
  51.     $page_total_count = ceil($element_total_count/$per_page_elem_count);
  52.     if(intval($element_total_count)< 1 || !isset($element_total_count)) {
  53.         return '';
  54.     }
  55.     if($element_total_count <= $per_page_elem_count)
  56.         return '';
  57.     if($current_page>$page_total_count)
  58.         $current_page = 1;
  59.     if(strpos($href,"#")) {
  60.         $label = substr($href,strpos($href,"#"));
  61.         $href = substr($href,0,strpos($href,"#"));
  62.     } else {
  63.     $label = '';
  64.     }
  65.     /* 生成页码 */
  66.     if($current_page > ceil($show_page_num/2)) {
  67.         $start = $current_page - ceil($show_page_num/2);
  68.         $end = (($current_page+ceil($show_page_num/2))<$page_total_count) ?
  69.             $current_page+ceil($show_page_num/2)-1 : $page_total_count;
  70.     } else {
  71.         $start = 1;
  72.         $end = ($show_page_num>$page_total_count) ? $page_total_count : $show_page_num;
  73.     }
  74.     if(!empty($curr_num_class))
  75.         $num_class_str = ' class="'.$curr_num_class.'"';
  76.     else
  77.         $num_class_str = '';
  78.     $page_num_string = '';
  79.     for($i=$start;$i<=$end;$i++) {
  80.         if(intval($i) == intval($current_page))
  81.             $page_num_string .= $a_container_pre.'.$num_class_str.'>'.$i.''.$a_container_suf;
  82.         else
  83.             $page_num_string .= $a_container_pre.'.$href.$prefix.'='.$i.$label.'">'.$i.''.$a_container_suf;
  84.     }
  85.     /* 上下翻页 */
  86.     $prev_page = (intval($current_page-1)>0)?intval($current_page-1):0;
  87.     $next_page = (intval($current_page)<$page_total_count) ? intval($current_page+1) : 0;
  88.     if(!empty($up_class))
  89.         $up_class_str = ' class="'.$up_class.'"';
  90.     else
  91.         $up_class_str = '';
  92.     $page_up_string = '';
  93.     if(intval($prev_page) > 0)
  94.         $page_up_string = $a_container_pre.'.$href.$prefix.'='.$prev_page.$label.'"'.$up_class_str.'>'.$up_text.''.$a_container_suf;
  95.     else
  96.         $page_up_string = $a_container_pre.'.$up_class_str.'>'.$up_text.''.$a_container_suf;
  97.     $page_down_string = '';
  98.     if(!empty($down_class))
  99.         $down_class_str = ' class="'.$down_class.'"';
  100.     else
  101.         $down_class_str = '';
  102.     if(intval($next_page) > 0)
  103.         $page_down_string .= $a_container_pre.'.$href.$prefix.'='.$next_page.$label.'"'.$down_class_str.'>'.$down_text.''.$a_container_suf;
  104.     else
  105.         $page_down_string .= $a_container_pre.'.$down_class_str.'>'.$down_text.''.$a_container_suf;
  106.     return $page_up_string . $page_num_string . $page_down_string;
  107. }


阅读(1735) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~