详见 http://qxblog.sinaapp.com/?p=100
- //$count为总条目数,$page为当前页码,$page_size为每页显示条目数
- function show_page($count,$page,$page_size)
- {
- $page_count = ceil($count/$page_size); //计算得出总页数
-
- $init=1;
- $page_len=7;
- $max_p=$page_count;
- $pages=$page_count;
-
- //判断当前页码
- $page=(empty($page)||$page //获取当前页url
- $url = $_SERVER['REQUEST_URI'];
- //去掉url中原先的page参数以便加入新的page参数
- $parsedurl=parse_url($url);
- $url_query = isset($parsedurl['query']) ? $parsedurl['query']:'';
- if($url_query != ''){
- $url_query = preg_replace("/(^|&)page=$page/",'',$url_query);
- $url = str_replace($parsedurl['query'],$url_query,$url);
- if($url_query != ''){
- $url .= '&';
- }
- } else {
- $url .= '?';
- }
-
- //分页功能代码
- $page_len = ($page_len%2)?$page_len:$page_len+1; //页码个数
- $pageoffset = ($page_len-1)/2; //页码个数左右偏移量
-
- $navs='';
- if($pages != 0){
- if($page!=1){
- $navs.="首页 "; //第一页
- $navs.="上页"; //上一页
- } else {
- $navs .= "首页";
- $navs .= "上页";
- }
- if($pages>$page_len)
- {
- //如果当前页小于等于左偏移
- if($page $init=1;
- $max_p = $page_len;
- }
- else //如果当前页大于左偏移
- {
- //如果当前页码右偏移超出最大分页数
- if($page+$pageoffset>=$pages+1){
- $init = $pages-$page_len+1;
- }
- else
- {
- //左右偏移都存在时的计算
- $init = $page-$pageoffset;
- $max_p = $page+$pageoffset;
- }
- }
- }
- for($i=$init;$i {
- if($i==$page){$navs.="".$i.'';}
- else {$navs.=" ".$i."";}
- }
- if($page!=$pages)
- {
- $navs.=" 下页 ";//下一页
- $navs.="末页"; //最后一页
- } else {
- $navs .= "下页";
- $navs .= "末页";
- }
- echo "$navs";
- }
- }
复制代码
|