首頁 >後端開發 >php教程 >PHP分页问题,请大神现身!分页代码页书显示正确,但每页显示重复内容。

PHP分页问题,请大神现身!分页代码页书显示正确,但每页显示重复内容。

WBOY
WBOY原創
2016-06-23 13:22:361586瀏覽


数据库内共有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

好像不行  其实最开始的代码是 $s = "select * from cp limit $select_from,$select_limit"; 
$select_from =($_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所以不会显示内容,我自己再改一下好了。
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn