这将是我第一次将基于条件的搜索与分页一起使用。
我想对我的搜索查询结果进行分页,并能够根据搜索条件转到我的搜索结果的下一页和上一页。现在分页返回表中的所有记录。我试图在 NEXT 和 PREVIOUS 按钮的 URL 链接中包含一个搜索词,但是当我单击按钮时,我仍然会在下一页加载 MySQL 表中的所有记录。搜索词(例如,gallery.php?cmaterial=$cmaterial)仅在我单击“下一步”而不是当前页面时出现在 URL 中。即使这样,搜索结果也与搜索词不匹配。
我试图从以前关于这个主题的帖子中学习,但我不确定我是不是错了。
这是我的代码:
<?php
$s=$_GET['s'];
// rows to return
$limit=12;
// check for a search parameter
//if (!isset($var))
// {
// echo "<p>We dont seem to have a search parameter!</p>";
// exit;
// }
$ctitle = mysql_real_escape_string($_POST['ctitle']);
$csubject = mysql_real_escape_string($_POST['csubject']);
$creference = mysql_real_escape_string($_POST['creference']);
$cat_id = ($_POST['cat_id']);
$cmaterial = mysql_real_escape_string($_POST['cmaterial']);
$ctechnic = mysql_real_escape_string($_POST['ctechnic']);
$cartist = mysql_real_escape_string($_POST['cartist']);
$csource = mysql_real_escape_string($_POST['csource']);
$stolen = mysql_real_escape_string($_POST['stolen']);
$sql = "SELECT * FROM collections WHERE c_id>0 AND `ctitle` LIKE '%".$ctitle."%' AND `csubject` LIKE '%".$csubject."%' AND `creference` LIKE '%".$creference."%' AND `cat_id` LIKE '%".$cat_id."%' AND `cmaterial` LIKE '%".$cmaterial."%' AND `ctechnic` LIKE '%".$ctechnic."%' AND `cartist` LIKE '%".$cartist."%' AND `csource` LIKE '%".$csource."%' ORDER BY c_id ASC";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$numresults=mysql_query($sql);
$numrows=mysql_num_rows($numresults);
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$sql .= " limit $s,$limit";
$sql_result = mysql_query($sql) or die("Couldn't execute query");
$count = 1 + $s ;
while ($row = mysql_fetch_assoc($sql_result)) {
$c_id=$row['c_id'];
?>
..............................................HTML RESULT TABLE ...................................................
<?php
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print " <a href=\"$PHP_SELF?s=$prevs&cmaterial=$cmaterial\"><<
Prev 10</a>  ";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
echo "</table>";
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo " <a href=\"$PHP_SELF?s=$news&cmaterial=$cmaterial\">Next 10 >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
?>