php分页

下面两个分页方式:

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 src="http://upcdn.b0.upaiyun.com/libs/jquery/jquery-1.8.2.min.js"></script>
<?php
// 使用示例
if (!$conn= mysql_connect("127.0.0.1", "aaa" ,"aaaa")) die('数据库选择失败!');
if (!mysql_select_db("aaaa", $conn)) die('数据库选择失败!');
mysql_query('set names utf-8');
// 用Page函数计算出 $select_from 从哪条记录开始检索、$pagenav 输出分页导航
$rows = mysql_num_rows(mysql_query("select * from aaa"));

?>
<script type="text/javascript">
$(function(){
var num=<?php echo isset($_GET["p"])?$_GET["p"]:1; ?>;
var str=setpage(<?=$rows?>,20,num);
$(".page").append(str);
/*
@maxpage 每页显示的页码数
@page 当前的页数
@count 总记录数
*/
function setpage(count,maxpage,page){
var str='';
if(page<=0){page=1;}
totalpage = Math.ceil(count/maxpage);
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++){
for(var i=start;i<=start+5;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>

<?php
// 使用示例

$p = $_GET["p"]?$_GET['p']:1;
$page_size = 20; //每页记录数
$select_from = ($p - 1) * $page_size;
$sql = "select id,title from aaa limit $select_from ,$page_size";
echo $sql.'<br>';
$rst = mysql_query($sql);
// var_dump(mysql_fetch_array($rst));
while ($row = mysql_fetch_array($rst)){
echo "$row[id] - $row[title]<hr />";
}
?>
<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

/**
* $rows 总页数
* $page 当前页号
* $page_size 当页显示的条数
**/


function Page($rows,$page,$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";
}
echo $pagenav;
} // Page分页函数


// 使用示例
if (!$conn= mysql_connect("127.0.0.1", "aaa" ,"aaa")) die('数据库选择失败!');
if (!mysql_select_db("aaa", $conn)) die('数据库选择失败!');
mysql_query('set names utf-8');
// 用Page函数计算出 $select_from 从哪条记录开始检索、$pagenav 输出分页导航
$rows = mysql_num_rows(mysql_query("select * from aaaa"));
$page = $_GET["page"]?$_GET['page']:1;
$page_size = 20; //每页记录数
$select_from = ($page - 1) * $page_size;
$sql = "select id,title from aaaa limit $select_from ,$page_size";
echo $sql.'<br>';
$rst = mysql_query($sql);
// var_dump(mysql_fetch_array($rst));
while ($row = mysql_fetch_array($rst)){
echo "$row[id] - $row[title]<hr />";
}
Page($rows,$page,$page_size);

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

 

posted @ 2017-03-09 21:53  阿锋佬  阅读(373)  评论(0编辑  收藏  举报