数据库内共有1287条记录,设置每页显示1000条,每行显示9条。
但结果一页里就把所有记录1~1287都显示了,第二页也是1~1287.
分页数是正确的2页,
哪里出了问题请各位帮忙看下代码,十分感谢!
代码如下:
<?php $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"; } } $conn = mysql_connect('127.0.0.1:8889', 'root', 'root');mysql_select_db('test3', $conn);$rows = mysql_num_rows(mysql_query("select * from cp")); $sql="select * from cp";$r = mysql_query($sql);Page($rows,1000); $s = "select * from cp limit 0,1000"; $rst = mysql_query($s); $row = mysql_fetch_array($rst); ?><table border="1" ><?php $k=1; while($rs = mysql_fetch_assoc($r)) { $color=$rs["count"] >= "3"?"#999999":"white"; echo $k%9==1?"<tr>":"";?> <td bgcolor="#3399FF"><?php echo $rs["id"];?></td> <td bgcolor="<?php echo $color;?>"><?php echo $rs["cp"];?></td> <td bgcolor="<?php echo $color;?>"><?php echo $rs["count"];?></td><?php echo $k%9==0?"</tr>":""; $k++; }?><?phpecho $pagenav;?></table>
你的查询串是 $s = "select * from cp limit 0,1000";
无论 $_GET["page"] 是什么,那个 0 总是不会变的,当然是每页都显示相同的结果啦
查询的起始偏移应该是与 $_GET["page"] 相关的数字
对于你的情况就是 ($_GET["page"] - 1) * 1000
你的查询串是 $s = "select * from cp limit 0,1000";
无论 $_GET["page"] 是什么,那个 0 总是不会变的,当然是每页都显示相同的结果啦
查询的起始偏移应该是与 $_GET["page"] 相关的数字
对于你的情况就是 ($_GET["page"] - 1) * 1000
<?php $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"; } } $conn = mysql_connect('127.0.0.1:8889', 'root', 'root');mysql_select_db('test3', $conn);$rows = mysql_num_rows(mysql_query("select * from cp")); ///@@这段帮你添加的 $limit_start=($page-1)*1000; $s = "select * from cp limit $limit_start,1000"; ///@@$sql="select * from cp";$r = mysql_query($s);Page($rows,1000); //$s = "select * from cp limit 0,1000"; //$rst = mysql_query($s); //$row = mysql_fetch_array($rst); ?><table border="1" ><?php $k=1; while($rs = mysql_fetch_assoc($r)) { $color=$rs["count"] >= "3"?"#999999":"white"; echo $k%9==1?"<tr>":"";?> <td bgcolor="#3399FF"><?php echo $rs["id"];?></td> <td bgcolor="<?php echo $color;?>"><?php echo $rs["cp"];?></td> <td bgcolor="<?php echo $color;?>"><?php echo $rs["count"];?></td><?php echo $k%9==0?"</tr>":""; $k++; } ?><?phpecho $pagenav; ?></table>
$sql="select * from cp limit ".($page-1)*1000.",1000";$r = mysql_query($sql);Page($rows,1000); //$s = "select * from cp limit 0,1000"; //$rst = mysql_query($s); //$row = mysql_fetch_array($rst); 上面三行你都没用到,注释掉$sql修改一下,你下面循环用到的是$r对应的sql语句所以一直是一样的
$sql="select * from cp limit ".($page-1)*1000.",1000";$r = mysql_query($sql);Page($rows,1000); //$s = "select * from cp limit 0,1000"; //$rst = mysql_query($s); //$row = mysql_fetch_array($rst); 上面三行你都没用到,注释掉$sql修改一下,你下面循环用到的是$r对应的sql语句所以一直是一样的
$cnt = ($page-1)*1000;$sql="select * from cp limit $cnt,1000";
<?php $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"; } } $conn = mysql_connect('127.0.0.1:8889', 'root', 'root');mysql_select_db('test3', $conn);$rows = mysql_num_rows(mysql_query("select * from cp")); ///@@这段帮你添加的 $limit_start=($page-1)*1000; $s = "select * from cp limit $limit_start,1000"; ///@@$sql="select * from cp";$r = mysql_query($s);Page($rows,1000); //$s = "select * from cp limit 0,1000"; //$rst = mysql_query($s); //$row = mysql_fetch_array($rst); ?><table border="1" ><?php $k=1; while($rs = mysql_fetch_assoc($r)) { $color=$rs["count"] >= "3"?"#999999":"white"; echo $k%9==1?"<tr>":"";?> <td bgcolor="#3399FF"><?php echo $rs["id"];?></td> <td bgcolor="<?php echo $color;?>"><?php echo $rs["cp"];?></td> <td bgcolor="<?php echo $color;?>"><?php echo $rs["count"];?></td><?php echo $k%9==0?"</tr>":""; $k++; } ?><?phpecho $pagenav; ?></table>谢谢回答 实际试了下 可以是可以只是初始打开时没指定page所以不会显示内容,我自己再改一下好了。