我的php脚本上有这个代码用于分页
<?php
session_start();
//Database stuff here
$image = $_SESSION['image'];
$cat = $_GET['cate'];
$pagenum = $_GET['pagenum'];
//PAGING BEGINS
if(!isset($pagenum)){
$pagenum = 1;
}
$data = mysql_query("SELECT * FROM tbl_product WHERE category = '$cat'");
$rows = mysql_num_rows($data);
$page_rows = 4;
$last = ceil($rows/$page_rows);
if($pagenum < 1)
$pagenum = 1;
elseif($pagenum > $last)
$pagenum = $last;
$max = 'limit '.($pagenum - 1) * $page_rows.','.$page_rows;
$data_p = mysql_query("SELECT * FROM tbl_product WHERE category = '$cat' $max");
while($row = mysql_fetch_array($data_p)) {
echo $row[1];
echo $row[2];
}
echo "<p>";
echo "--Page $pagenum of Last </p>";
if($pagenum == 1){
} else {
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=1'>First</a>";
echo "";
$previous = $pagenum - 1;
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'><-Previous</a>";
}
if($pagenum == $last){
} else {
$next = $pagenum + 1;
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next ->></a>";
echo "";
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last</a>";
}
?>
当我运行它时,第一页显示指定的4项,但是当我单击“下一步”时,$ pagenum变量变为0,导致限制($ max)的负范围,因此出现SQL错误。我不知道为什么。我已经尝试了几个分页脚本,最终出现了同样的错误。当我尝试移动到下一页时,$ pagenum变为0。请问我的错误是什么?谢谢
答案 0 :(得分:2)
想想我知道这个问题,你有:
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last</a>";
问题可能是$_SERVER['PHP_SELF']
返回index.php然后你在?pagenum=1
添加,因此你得到的网址是:
/index.php?pagenum=1
所以你丢失了你的类别变量。尝试将所有链接更改为以下内容:
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$last&cate=$cat'>Last</a>";