頁面跳轉越界問題,求老師解答
<?php
/**分頁模塊**/
$p=empty($_GET['p'])?1:$_GET['p'];//獲取頁碼;
$table='cms_p';//數據庫表名稱
/**php->mysql**/
$mysqli=?new?mysqli('localhost','root','5866138','php_cms');
if($mysqli->errno)
{
die('錯誤號是:'.$mysqli->errno.'錯誤信息:'.$mysqli->error);
}
$mysqli->set_charset('utf8');
$sql='select?count(id)?as?c?from?cms_p;';
$res=$mysqli->query($sql);
if(!$res)
{
die('查詢失敗'.$mysqli->error);
}
else?if(is_object($res))
{
if($res->num_rows>0)
{
$row1=$res->fetch_all(MYSQLI_ASSOC);
$res->free();
}else?{?echo?"沒有結果";
$row1=1;}
}
/**分頁設置**/
$pagesize=10;//每頁的數據量;
$pagecount=ceil($row1[0]['c']/$pagesize);//頁面的總數量;
$limit=($p-1)*10;
/**數據處理模塊**/
$sql="select?*?from?cms_p?limit?$limit?,?$pagesize?;";
$res2=$mysqli->query($sql);
if(!$res2)
{
die('查詢失敗'.$mysqli->error);
}
else?if(is_object($res2))
{
if($res2->num_rows>0)
{
$row2=$res2->fetch_all(MYSQLI_ASSOC);
$res2->free();
}else?{?echo?"沒有結果";}
}
/**數據顯示模塊**/
echo?'<table?border="1"?cellspacing="0"?cellpadding="6">
<caption>CMS用戶管理列表</caption>
<tr>
<th>編號</th>
<th>實際id</th>
<th>用戶名</th>
<th>密碼</th>
<th>年齡</th>
<th>性別</th>
<th>權限</th>
<th>注冊ip地址</th>
<th>注冊時間</th>
<th>操作</th>
</tr>';
if($row1==1)
{
echo?'<tr><td?colspan="10">沒有結果!不能顯示!</td></tr>';
}else?
{
$i=1;
foreach($row2?as?$key=>$val)
{
echo?'<tr>';
echo?'<td>'.$i.'</td>';
echo?'<td>'.$val['id'].'</td>';
echo?'<td>'.$val['uname'].'</td>';
echo?'<td>'.$val['psd'].'</td>';
echo?'<td>'.$val['age'].'</td>';
echo?'<td>'.$val['sex'].'</td>';
echo?'<td>'.$val['po'].'</td>';
echo?'<td>'.$val['rip'].'</td>';
echo?'<td>'.$val['rtime'].'</td>';
echo?'<td><a?href="'.$val['id'].'">修改</a></td>';
echo?'</tr>';
$i++;
}
}
echo?'</table>';
/**分頁操作模塊**/
echo?'<div>';
if($p<=1){
$p=1;
}
if($p>=$pagecount)
{
$p=$pagecount;
}
if($p>1)
{
echo?'<a?href="?p=1">?首頁?</a>';
echo?'<a?href="?p='.($p-1).'">?<上一頁?</a>';
}
if($pagecount<=10)
{
for($j=1;$j<=10;$j++)
{
echo?'<a?href="?p='.$j.'">?'.$j.'?</a>';
}
}else
{
if($p>=6&&$p<=$pagecount-6)
{
echo?'...';
for($j=$p-4;$j<=$p+5;$j++)
{
echo?'<a?href="?p='.$j.'">?'.$j.'?</a>';
}
echo?'...';
}
else?if($p>$pagecount-6&&$p<=$pagecount)
{
echo?'...';
for($j=$pagecount-7;$j<=$pagecount;$j++)
{
echo?'<a?href="?p='.$j.'">?'.$j.'?</a>';
}
}
else?
{
for($j=1;$j<=10;$j++)
{
echo?'<a?href="?p='.$j.'">?'.$j.'?</a>';
}
echo?'...';
}
}
if($p<$pagecount)
{
echo?'<a?href="?p='.($p+1).'">?下一頁>?</a>';
echo?'<a?href="?p='.$pagecount.'">?尾頁?</a>';
}
echo?'?共'.$pagecount.'頁'.'當前第?'.$p.'?頁';
echo?'到第<form?action="page.php?p='.$p.'"?><input?type="number"?name="p"?size=2?/>?<input?type="submit"?value="跳轉"?/></form>';
echo?"</div>";
?>比如我的頁數只有20頁,然后我輸入100,跳轉的時候會出錯,寫的過濾越界代碼沒有生效,求解為什么?
2015-07-21
我沒有細看這樣行不行
$p = min($p, $pagecount);
2015-07-21
我試了
if($p<=1){ $p=1; } if($p>=$pagecount) { $p=min($p,$pagecount); }這樣還是不行啊.不知道為什么不走這段代碼....我也沒有分析工具............