码迷,mamicode.com
首页 > 其他好文 > 详细

分页块

时间:2016-04-18 15:15:51      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

<?php
//分页函数
function page($total_records,$page_size,$page_current,$url,$keyword){
 $total_pages=ceil($total_records/$page_size);//总共多少页=总共多少条记录/一页多少条
 $page_previous=($page_current<=1)?1:$page_current-1;
    $page_next=($page_current>=$total_pages)?$total_pages:$page_current+1;
    $page_next=($page_next==0)?1:$page_next;
 $page_start=($page_current-5>0)?$page_current-5:0;
 $page_end=($page_start+10<$total_pages)?$page_start+10:$total_pages;
    $page_start=$page_end-10;
 if($page_start<0) $page_start=0;
 //判断$url中是否存在查询字符串
    $parse_url=parse_url($url);
 if(empty($parse_url["query"])){
  $url=$url.‘?‘;//若不存在,在$url后添加?
 }else{
  $url=$url.‘&‘;
 }
 if(empty($keyword)){
  $navigator="<a href=".$url."page_current=$page_previous>上一页</a>";
        for($i=$page_start;$i<$page_end;$i++){
   $j=$i+1;
   $navigator.="<a href=‘".$url."page_current=$j‘>$j</a>";
  }
        $navigator.="<a href=".$url."page_current=$page_next>下一页</a>";
  $navigator.="<br/>共".$total_records."条记录,共".$total_pages."页,当前是第".$page_current."页";
 }
 else{
  $keyword=$_GET["keyword"];
  $navigator="<a href=".$url."keyword=$keyword&page_current=$page_previous>上一页</a>";
        for($i=$page_start;$i<$page_end;$i++){
   $j=$i+1;
   $navigator.="<a href=‘".$url."keyword=$keyword&page_current=$j‘>$j</a>";
  }
        $navigator.="<a href=".$url."keyword=$keyword&page_current=$page_next>下一页</a>";
  $navigator.="<br/>共".$total_records."条记录,共".$total_pages."页,当前是第".$page_current."页";
 }
 echo $navigator;

 /*改进前的:
  if(empty($keyword)){
  $navigator="<a href=$url?page_current=$page_previous>上一页</a>";
        for($i=$page_start;$i<$page_end;$i++){
   $j=$i+1;
   $navigator.="<a href=‘$url?page_current=$j‘>$j</a>";
  }
        $navigator.="<a href=$url?page_current=$page_next>下一页</a>";
  $navigator.="<br/>共".$total_records."条记录,共".$total_pages."页,当前是第".$page_current."页";
 }
 else{
  $keyword=$_GET["keyword"];
  $navigator="<a href=$url?keyword=$keyword&page_current=$page_previous>上一页</a>";
        for($i=$page_start;$i<$page_end;$i++){
   $j=$i+1;
   $navigator.="<a href=‘$url?keyword=$keyword&page_current=$j‘>$j</a>";
  }
        $navigator.="<a href=$url?keyword=$keyword&page_current=$page_next>下一页</a>";
  $navigator.="<br/>共".$total_records."条记录,共".$total_pages."页,当前是第".$page_current."页";
 }
 echo $navigator;*/
}
?>

 

分页块

标签:

原文地址:http://www.cnblogs.com/www0911--/p/5404094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!