码迷,mamicode.com
首页 > Web开发 > 详细

php分页

时间:2017-03-09 22:39:47      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:google   size   htm   tar   span   xhtml   org   roo   输出   

<?php 
// Page分页函数 
$page = $_GET["page"]; 
function Page($rows,$page_size){ 
global $page,$select_from,$select_limit,$pagenav; 
$page_count = ceil($rows/$page_size); 
if($page <= 1 || $page == ‘‘) $page = 1; 
if($page >= $page_count) $page = $page_count; 
$select_limit = $page_size; 
$select_from = ($page - 1) * $page_size.‘,‘; 
$pre_page = ($page == 1)? 1 : $page - 1; 
$next_page= ($page == $page_count)? $page_count : $page + 1 ; 
$pagenav .= "第 $page/$page_count 页 共 $rows 条记录 "; 
$pagenav .= "<a href=‘?page=1‘>首页</a> "; 
$pagenav .= "<a href=‘?page=$pre_page‘>前一页</a> "; 
$pagenav .= "<a href=‘?page=$next_page‘>后一页</a> "; 
$pagenav .= "<a href=‘?page=$page_count‘>末页</a>"; 
$pagenav.=" 跳到<select name=‘topage‘ size=‘1‘ onchange=‘window.location=\"?page=\"+this.value‘>\n"; 
for($i=1;$i<=$page_count;$i++){ 
if($i==$page) $pagenav.="<option value=‘$i‘ selected>$i</option>\n"; 
else $pagenav.="<option value=‘$i‘>$i</option>\n"; 
} 
} // Page分页函数 
// 使用示例 
if (!$conn= mysql_connect("localhost", "root" ,"root")) die(‘数据库选择失败!‘); 
if (!mysql_select_db("test", $conn)) die(‘数据库选择失败!‘); 
mysql_query(‘set names GBK‘); 
// 用Page函数计算出 $select_from 从哪条记录开始检索、$pagenav 输出分页导航 
$rows = mysql_num_rows(mysql_query("select * from test")); 
Page($rows,2); 
$sql = "select * from test limit $select_from $select_limit"; 
$rst = mysql_query($sql); 
while ($row = mysql_fetch_array($rst)){ 
echo "$row[id] - $row[sex] - $row[name] <hr />"; 
} 
echo $pagenav; 
?> 

 

 

下面两个分页方式:

js分页:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>分页代码</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    var num=<?php echo isset($_GET["p"])?$_GET["p"]:1; ?>;
    var str=setpage(7,num,48);
    $(".page").append(str);   
    /*
    @maxpage   每页显示的页码数
    @page      当前的页数
    @totalpage 总页数
    */
   function setpage(maxpage,page,totalpage){
      var str=‘‘;
      if(page<=0){page=1;}
      if(page>totalpage){page=totalpage;}

         var step1=page%maxpage;
         var start=page-step1;
         if(start<=0){start=1};
         var step2=maxpage-page%maxpage-1;
         var end=page+step2;
          if(totalpage<end){
            end=totalpage;
          }
          var prevpage=page-1;
          if(prevpage<=0){prevpage=1};
          var nextpage=page+1;
          if(nextpage>totalpage){
            nextpage=totalpage;
          }
          if(page!=1){
            var str="<a href=‘?p=1‘>首页</a>";
            str+="<a href=‘?p="+prevpage+"‘>上页</a>";
          }
          for(var i=start;i<=end;i++){

             if(i==page){
                str+="<a href=‘?p="+i+"‘ class=‘cur‘>"+i+"</a>";
              }else{
                str+="<a href=‘?p="+i+"‘>"+i+"</a>";
              }
          }
          if(page!=totalpage){
            str+="<a href=‘?p="+nextpage+"‘>下页</a>";
            str+="<a href=‘?p="+totalpage+"‘>尾页</a>";
          }
           
          //$(".page").append(str);
          return str;
   }

})
</script>
</head>
<style>
*{padding: 0;margin:0;list-style: none}
ul,li{list-style: none;}
.page{width: 850px;height: 50px;margin: 20px auto;border: 0px solid #ccc;padding-top: 30px;}
.page a{padding: 5px 8px;text-align: center;border: 1px solid #ccc;margin:10px;text-decoration: none;}
.page a:hover{background: #ccc;color: #fff}
.page .cur{background: #ccc;color: #fff}
</style>
<body>
 <div class="page">
 </div>
</body>
</html>

php分页:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>图片滚动</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<style>
*{padding: 0;margin:0;list-style: none}
ul,li{list-style: none;}
.page{width: 850px;height: 50px;margin: 20px auto;border: 0px solid #ccc;padding-top: 30px;}
.page a{padding: 5px 8px;text-align: center;border: 1px solid #ccc;margin:10px;text-decoration: none;}
.page a:hover{background: #ccc;color: #fff}
.page .cur{background: #ccc;color: #fff}
</style>
<body>
 <div class="page">
  <?php

/**
 * $count 总页数
 * $page 当前页号
 * $num 显示的页码数
 **/
function pagebar($count, $page, $num) {
  $num = min($count, $num); //处理显示的页码数大于总页数的情况
  if($page > $count || $page < 1) return; //处理非法页号的情况
  $end = $page + floor($num/2) <= $count ? $page + floor($num/2) : $count; //计算结束页号
  $start = $end - $num + 1; //计算开始页号
  if($start < 1) { //处理开始页号小于1的情况
    $end -= $start - 1;
    $start = 1;
  }
  
  $prve=$page-1;
  $next=$page+1;

  if($page!=1){
    echo "<a href=‘?p=1‘>首页</a>";
    echo "<a href=‘?p=".$prve."‘>上页</a>";
  }
   
  for($i=$start; $i<=$end; $i++) { //输出分页条,请自行添加链接样式
    if($i == $page) echo "<a href=‘?p=$i‘ class=‘cur‘>$i</a>";
    else echo " <a href=‘?p=$i‘>$i</a> ";
  }
  if($page!=$count){
    echo "<a href=‘?p=".$next."‘>下页</a>";
    echo "<a href=‘?p=".$count."‘>尾页</a>";
  }
   
}
 
 $p=isset($_GET[‘p‘])?$_GET[‘p‘]:1;
 pagebar(28, $p, 8);

?>
 </div>
</body>
</html>

 

php分页

标签:google   size   htm   tar   span   xhtml   org   roo   输出   

原文地址:http://www.cnblogs.com/feng18/p/6528094.html

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