我写了段代码,想从数据库中调出是“计算机”类的图书,然后将它分页显示,可以能显示第一页,但显示不了下一页,好像是第二页没数据似的,(共查出8本计算机类的图书,每页显示3本啊)我的代码是:<?php
$link=mysql_connect("$localhost","$root","$root")||die("无法连接数据库!");
$db=mysql_select_db("test")||die("选择数据库失败!");
$type=$_POST["select"];
if($page==""){$page=1;};
if($page){
$page_size=3;
$sql1="select count(*) as total from db_book where type='".$type."'";
$result1=mysql_query($sql1);
$message_count=mysql_result($result1,0,"total");
$page_count=ceil($message_count/$page_size);
$offset=($page-1)*$page_size;
$sql2="select * from db_book where type='".$type."' limit $offset,$page_size";
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($result2)){
?> <tr>
<td width="90"><div align="center" class="STYLE2">第<?php echo $rows[id]; ?>条</div></td>
<td width="350"><div align="center" class="STYLE2"><?php echo $rows[bookname]; ?></div></td>
<td width="90"><div align="center" class="STYLE2"><?php echo $rows[type]; ?></div></td>
<td width="200"><div align="center" class="STYLE2"><?php echo $rows[zuozhe]; ?></div></td>
<td width="280"><div align="center" class="STYLE2"><?php echo $rows[cbs]; ?></div></td>
<td width="90"><div align="center" class="STYLE2"><?php echo $rows[price]; ?></div></td>
</tr>
<?php } } ?>
<tr>
<td colspan="6"><div align="center" class="STYLE2">图书统计:<?php echo $message_count; ?>本 共:<?php echo $page_count; ?>页 每页显示:<?php echo $page_size;?>条 分页:
<?php if($page!=1){
echo "<a href=search.php?page=1>首页</a> ";
echo "<a href=search.php?page=".($page-1).">上一页</a> ";
}
if($page<$page_count){
echo "<a href=search.php?page=".($page+1).">下一页</a> ";
echo "<a href=search.php?page=".$page_count.">尾页</a>";
}
?> </div></td>
</tr>
$link=mysql_connect("$localhost","$root","$root")||die("无法连接数据库!");
$db=mysql_select_db("test")||die("选择数据库失败!");
$type=$_POST["select"];
if($page==""){$page=1;};
if($page){
$page_size=3;
$sql1="select count(*) as total from db_book where type='".$type."'";
$result1=mysql_query($sql1);
$message_count=mysql_result($result1,0,"total");
$page_count=ceil($message_count/$page_size);
$offset=($page-1)*$page_size;
$sql2="select * from db_book where type='".$type."' limit $offset,$page_size";
$result2=mysql_query($sql2);
while($rows=mysql_fetch_array($result2)){
?> <tr>
<td width="90"><div align="center" class="STYLE2">第<?php echo $rows[id]; ?>条</div></td>
<td width="350"><div align="center" class="STYLE2"><?php echo $rows[bookname]; ?></div></td>
<td width="90"><div align="center" class="STYLE2"><?php echo $rows[type]; ?></div></td>
<td width="200"><div align="center" class="STYLE2"><?php echo $rows[zuozhe]; ?></div></td>
<td width="280"><div align="center" class="STYLE2"><?php echo $rows[cbs]; ?></div></td>
<td width="90"><div align="center" class="STYLE2"><?php echo $rows[price]; ?></div></td>
</tr>
<?php } } ?>
<tr>
<td colspan="6"><div align="center" class="STYLE2">图书统计:<?php echo $message_count; ?>本 共:<?php echo $page_count; ?>页 每页显示:<?php echo $page_size;?>条 分页:
<?php if($page!=1){
echo "<a href=search.php?page=1>首页</a> ";
echo "<a href=search.php?page=".($page-1).">上一页</a> ";
}
if($page<$page_count){
echo "<a href=search.php?page=".($page+1).">下一页</a> ";
echo "<a href=search.php?page=".$page_count.">尾页</a>";
}
?> </div></td>
</tr>
$type=$_POST["select"];
改为
$type=$_REQUEST["select"]; 2.
传递查询类别
echo " <a href=search.php?page=".($page-1)."&type=$type>上一页 </a> ";
其他类同
<!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>
<meta http-equiv="refresh" content="30; url=showview.php?pg=1"> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>简易留言板</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.STYLE1 {
font-family: "黑体";
font-size: 36px;
color: #0000FF;
}
-->
</style>
</head> <body> <div class="main STYLE1"><marquee>简易留言板</marquee></div>
<div class="top1">
<table width="776" border="0.5">
<tr>
<td width="159"><div align="center"><a href="unlogin.php">返回首页</a></div></td>
<td width="165" nowrap="nowrap"><div align="center"><a href="message.php">返回留言页</a></div></td>
<td width="150"><div align="center"><a href="unlogin.php">退出登录</a></div></td>
<td width="137"><div align="center"></div></td>
<td width="143"><div align="right"><a href="showview.php?pg=1">查看留言</a></div></td>
</tr>
</table>
</div>
<div class="main2">
<?php
if(isset($_SESSION["username"]) && isset($_SESSION["password"])){
$user=$_SESSION["username"];
$pwd=$_SESSION["password"];
}else{
$user="";
$pwd="";
}
$link=mysql_connect("localhost","root","19881017")or die("数据库连接失败!请稍候再试!谢谢".mysql_error());//设置连接参数
mysql_select_db('db_message',$link);//选择数据库
$sql2="select * from tb_message order by id desc";//查询语句
$result=mysql_query($sql2,$link);//获取结果集 通过传入参数$sql2和$link得到
$row=mysql_num_rows($result);
$pagesize=3;//设置分页显示的单页显示数目
$pagecount=ceil($row/$pagesize);//设置页面总数
if(mysql_num_rows($result)) {//取得结果集的数目
$a=1;//定义变量 此变量是页面显示的留言条数
$page=$_REQUEST['pg'];
if(empty($page)||$page<1) {$page=1;}//判断页面号是否小于1,如果小于1则显示第一页
if($page>$pagecount) {$page=$pagecount;}//当页面号大于总页数时
$startrow=($page-1)*$pagesize;
$result2=mysql_query("select * from tb_message limit $startrow,$pagesize",$link);//设置游标,定位在$startrow=($page-1)*$pagesize处
//页面号-1以后乘单页显示数目,如果页面号是2则从第6个记录开始。后面的$pagesize的则是查询的记录数 while($row=mysql_fetch_array($result2)) {//当有记录的时候,循环输出结果
echo "第".$a."条:"."<br>";
echo "标题:".$row["title"]."<br>";
echo "留言者:".$row["name"]."<br>";
echo "内容:".$row["context"]."<br><hr>";
echo "来自:".$row["ip"]."<hr>";
echo "管理员回复:".$row["return1"]."<br><hr>";
if(isset($user)){
$user="admin";
}
if($user=="admin" && $pwd=="jcoffee1988+"){
echo "<form name="."form1"." action="."delete.php"." method="."post".">";
echo "<input type='hidden' value='".$row['id']."' name='id2'/>";
echo "<input type="."submit"." value="."删除此留言"." name="."submit"."/>";
echo "</form>";
echo "<form name="."form2"." action="."chenge.php"." method="."post".">";
echo "<input type="."submit"." value="."修改此留言内容"." name="."submit"."/>";
echo "<input type='text' name='context'/>";
echo "<input type='hidden' value='".$row['id']."' name='id'/>";
echo "</form>";
echo "<form name="."form3"." action="."return1.php"." method="."post".">";
echo "<input type="."submit"." value="."管理员回复"." name="."submit"."/>";
echo "<input type='text' name='return1'/>";
echo "<input type='hidden' value='".$row['id']."' name='id'/>";
echo "</form>";
}
echo "<hr>";
echo "<hr>";
$a++;//显示的记录号自增
}
}else {
$sql3="truncate table tb_message";//让数据库中的ID归零
mysql_query($sql3,$link);
echo "没有人留言!你坐沙发吗?";
}
?>
<div class="di" align="center"><a href="?pg=1" style="text-decoration:none">【首页】</a>
<a href="?pg=<?php if($_GET["pg"]==1)echo $page ;else echo $page-1 ;?>" style="text-decoration:none">上一页 </a>
<a href="?pg=<?php echo $page+1 ;?>" style="text-decoration:none">下一页</a>
<a href="?pg=<?php echo $pagecount ;?>" style="text-decoration:none">【尾页】</a>
共<?php echo $pagecount ?>页
</div>
</body>
</html>这个是我老早就写过的一个分页 参考下