【问题标题】:Linking searches together in php在php中将搜索链接在一起
【发布时间】: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


【解决方案1】:

如果我正确理解了这个问题,您可能希望能够搜索与您选中的复选框匹配的所有结果。

我不确定您到底要寻找什么结果,所以我会为您提供几个不同的选项,然后让您从那里获取。

首先,为了将多个复选框传递给您的 PHP 脚本,您需要以不同的方式定义您的复选框。

<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=">>"/>

注意 "search" .. name = "search[]" 后面的括号。这允许您的复选框传递一组值,而不仅仅是一个。

其次,在您的 PHP 方面。

提交复选框后,您将希望以与当前相同的方式获取它们:

$searches = $_POST['search'];

$searches 现在将是一个数组,其中填充了在您的表单中检查的值。

根据您想要的结果,您接下来要执行的操作将如下所示。

让我们以检查 A 和 B 的情况为例。

如果您想获得单词以字母 A 开头或单词以字母 B 开头的所有结果:

<?php 

    $searches = $_POST['searches'];
    $Squery = "SELECT * FROM database";
    $Swhere = ""; 

    //add all your individual searches by looping through the checkbox values
    foreach($searches as $search){
        $Swhere .= " OR name LIKE '%$search' ";
    }

    //remove the first OR case, because it is not needed for the first search query
    $Swhere = ltrim($Swhere, ' OR');

    //add your dynamic where statement to your query
    $Squery .= $Swhere

    $result = mysql_query($Squery);
    //...... run the query and get the results the same way you are

通过使用 foreach 函数,您可以将所有想要的复选框添加到搜索中,但 PHP 端的查询根本不需要更改。

类似地,在选择 A 和选择 B 的情况下,如果你想获取所有以 A 开头、以 B 开头或以 AB 开头的单词,那么你的代码将被调整如下:

<?php 

    $searches = $_POST['searches'];

    //this takes your array of values and combines them into one word.
    //your searches variable remains an array, but your stringSearch is now
    //a word with the combined values
    $stringSearch = implode($searches,'');

    $Squery = "SELECT * FROM database";
    $Swhere = " name LIKE '%$stringSearch%' "; 

    //add all your individual searches by looping through the checkbox values
    foreach($searches as $search){
        $Swhere .= " OR name LIKE '%$search' ";
    }

    //add your dynamic where statement to your query
    $Squery .= $Swhere

    $result = mysql_query($Squery);
    //...... run the query and get the results the same way you are

希望您可以采用此示例并根据您的实际需求进行调整。

需要注意的一点是,您不应该使用 mysql_query。它使您的代码面临主要的 SQL 注入问题。我只是在上面使用它来显示您将如何使用当前代码进行操作。查看mysqli 函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 2011-12-06
    相关资源
    最近更新 更多