【发布时间】:2015-04-15 23:04:48
【问题描述】:
我想将复选框表单中的搜索链接在一起。例如,如果我检查了 a 和 b,我有 5 个复选框(a、b、c、d、e),我想根据我搜索的内容进行搜索,即显示两个不同的结果。
<form role="form" action="R.php" method="post">
<div class="form-group">
<input type="checkbox" value="a" name="search" />a
<input type="checkbox" value="b" name="search" />b
<input type="checkbox" value="c" name="search" />c
<input type="checkbox" value="d" name="search" />d
<input type="checkbox" value="e" name="search" />e
</div>
<input class="btn btn-default glyphicon-align-right" type="submit" value=">>"/>
</form>
PHP
<?php $output='';
if(isset($_POST['search'])){
$searchq=$_POST['search'];
$searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);
$Squery = mysql_query("SELECT * FROM database WHERE Name LIKE '%$searchq%'");
$count = mysql_num_rows ($Squery);
echo "<div id=results><ul class=list-group><div id=qwerty>";
if($count == 0){
$output = 'There was no results!';
}else{
while($row = mysql_fetch_array($Squery)){
$name = $row ['Name'];
$id = $row['ID'];
$category = $row['Category'];
$output .='<div class="container">'.$name.$category.' '.$id.'</div>';
echo "<li>"."<div style=height:95px ><h3 class=text-center style=text-transform:capitalize >".$name.' '."</h3></div>".
"<div>"."<a href=n.php><img src=images/$id.jpg alt=Image /></a>
</div>".
' '.$category.' RESULT!!!!!'.$id."</li>";
}
echo "</div></ul></div>";
}
}
?>
【问题讨论】:
-
如果a、b、c都被选中,你要搜索where name = '%abc%' 还是 where name = a OR name = b OR name = c?
-
当 a、b 和 c 都被选中时。例如,选择字母 A 会给我一些以字母 A 开头的结果,选择 B 会给我一些以字母 B 结尾的结果,但是如果我检查 A 和 B,我想要以字母 A 和字母 B 开头的结果。这只是一个示例这种作品
-
找到了可能是一次只能读一个字的原因……但还是找不到解决办法。 :)
-
我认为您想要使用的是 SQL 中的 OR 语句。因此,对于选择“abc”的实例,name LIKE %abc% OR name LIKE %b OR LIKE name like a%
-
您是否也知道如何使用搜索字段来执行此操作...当我搜索“a b”时...我不阅读 b 而只阅读 a(基本上是我输入的第一个单词搜索字段)。我不想写 LIKE %a OR %b,因为稍后我的搜索会比这大得多,它会让我写很多 %etc 的搜索量更大。感谢您的帮助:)
标签: php html mysql forms checkbox