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

WBOY
풀어 주다: 2016-06-23 13:22:36
원래의
1523명이 탐색했습니다.


数据库内共有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所以不会显示内容,我自己再改一下好了。
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!