Home  >  Article  >  Backend Development  >  PHP分页有关问题,请大神现身!分页代码页书显示正确,但每页显示重复内容

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

WBOY
WBOYOriginal
2016-06-13 12:27:351370browse

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

数据库内共有1287条记录,设置每页显示1000条,每行显示9条。

但结果一页里就把所有记录1~1287都显示了,第二页也是1~1287.
分页数是正确的2页,

哪里出了问题请各位帮忙看下代码,十分感谢!
代码如下:

<?php <br />$page = $_GET["page"]; <br />function Page($rows,$page_size){ <br />global $page,$select_from,$select_limit,$pagenav; <br />$page_count = ceil($rows/$page_size); <br />if($page <= 1 || $page == '') $page = 1; <br />if($page >= $page_count) $page = $page_count; <br />$select_limit = $page_size; <br />$select_from = ($page - 1) * $page_size.','; <br />$pre_page = ($page == 1)? 1 : $page - 1; <br />$next_page= ($page == $page_count)? $page_count : $page + 1 ; <br />$pagenav .= "第 $page/$page_count 页 共 $rows 条记录 "; <br />$pagenav .= "<a href='?page=1'>首页</a> "; <br />$pagenav .= "<a href='?page=$pre_page'>前一页</a> "; <br />$pagenav .= "<a href='?page=$next_page'>后一页</a> "; <br />$pagenav .= "<a href='?page=$page_count'>末页</a>"; <br />$pagenav.=" 跳到<select name='topage' size='1' onchange='window.location=\"?page=\"+this.value'>\n"; <br />for($i=1;$i<=$page_count;$i++){ <br />if($i==$page) $pagenav.="<option value='$i' selected>$i</option>\n"; <br />else $pagenav.="<option value='$i'>$i</option>\n"; <br />} <br />} <br />$conn = mysql_connect('127.0.0.1:8889', 'root', 'root');<br />mysql_select_db('test3', $conn);<br />$rows = mysql_num_rows(mysql_query("select * from cp")); <br />$sql="select * from cp";<br />$r = mysql_query($sql);<br />Page($rows,1000); <br />$s = "select * from cp limit 0,1000"; <br />$rst = mysql_query($s); <br />$row = mysql_fetch_array($rst); <br />?><br /><table border="1" ><br /><?php<br /><br />  $k=1;<br />  while($rs = mysql_fetch_assoc($r))<br />  {<br />    $color=$rs["count"] >= "3"?"#999999":"white";<br />    echo $k%9==1?"<tr>":"";<br />?><br />   <td bgcolor="#3399FF"><?php echo $rs["id"];?></td><br />   <td bgcolor="<?php echo $color;?>"><?php echo $rs["cp"];?></td><br />   <td bgcolor="<?php echo $color;?>"><?php echo $rs["count"];?></td><br /><?php<br />   echo $k%9==0?"</tr>":"";<br />   $k++;<br />  }<br /><br />?><br /><?php<br />echo $pagenav;<br /><br />?><br /></table>




------解决思路----------------------
你的查询串是 $s?=?"select?*?from?cp?limit?0,1000";
无论 $_GET["page"] 是什么,那个 0 总是不会变的,当然是每页都显示相同的结果啦
查询的起始偏移应该是与 $_GET["page"] 相关的数字
对于你的情况就是 ($_GET["page"] - 1) * 1000


------解决思路----------------------
<br /><br /><?php <br />$page = $_GET["page"]; <br />function Page($rows,$page_size){ <br />global $page,$select_from,$select_limit,$pagenav; <br />$page_count = ceil($rows/$page_size); <br />if($page <= 1 <br><font color='#FF8000'>------解决思路----------------------</font><br> $page == '') $page = 1; <br />if($page >= $page_count) $page = $page_count; <br />$select_limit = $page_size; <br />$select_from = ($page - 1) * $page_size.','; <br />$pre_page = ($page == 1)? 1 : $page - 1; <br />$next_page= ($page == $page_count)? $page_count : $page + 1 ; <br />$pagenav .= "第 $page/$page_count 页 共 $rows 条记录 "; <br />$pagenav .= "<a href='?page=1'>首页</a> "; <br />$pagenav .= "<a href='?page=$pre_page'>前一页</a> "; <br />$pagenav .= "<a href='?page=$next_page'>后一页</a> "; <br />$pagenav .= "<a href='?page=$page_count'>末页</a>"; <br />$pagenav.=" 跳到<select name='topage' size='1' onchange='window.location=\"?page=\"+this.value'>\n"; <br />for($i=1;$i<=$page_count;$i++){ <br />if($i==$page) $pagenav.="<option value='$i' selected>$i</option>\n"; <br />else $pagenav.="<option value='$i'>$i</option>\n"; <br />} <br />} <br />$conn = mysql_connect('127.0.0.1:8889', 'root', 'root');<br />mysql_select_db('test3', $conn);<br />$rows = mysql_num_rows(mysql_query("select * from cp")); <br />///@@这段帮你添加的<br />	$limit_start=($page-1)*1000;<br />	$s = "select * from cp limit $limit_start,1000"; <br />///@@<br /><br />$sql="select * from cp";<br />$r = mysql_query($s);<br />Page($rows,1000); <br />//$s = "select * from cp limit 0,1000"; <br />//$rst = mysql_query($s); <br />//$row = mysql_fetch_array($rst); <br />?><br /><table border="1" ><br /><?php<br /> <br />  $k=1;<br />  while($rs = mysql_fetch_assoc($r))<br />  {<br />    $color=$rs["count"] >= "3"?"#999999":"white";<br />    echo $k%9==1?"<tr>":"";<br />?><br />   <td bgcolor="#3399FF"><?php echo $rs["id"];?></td><br />   <td bgcolor="<?php echo $color;?>"><?php echo $rs["cp"];?></td><br />   <td bgcolor="<?php echo $color;?>"><?php echo $rs["count"];?></td><br /><?php<br />   echo $k%9==0?"</tr>":"";<br />   $k++;<br />  }<br /> <br />?><br /><?php<br />echo $pagenav;<br /> <br />?><br /></table><br />

------解决思路----------------------
<br /><br />$sql="select * from cp limit ".($page-1)*1000.",1000";<br />$r = mysql_query($sql);<br />Page($rows,1000); <br />//$s = "select * from cp limit 0,1000"; <br />//$rst = mysql_query($s); <br />//$row = mysql_fetch_array($rst); <br />上面三行你都没用到,注释掉<br />$sql修改一下,你下面循环用到的是$r对应的sql语句<br />所以一直是一样的<br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn