【问题标题】:loop through database and narrow search by using the returned id values使用返回的 id 值循环访问数据库并缩小搜索范围
【发布时间】:2016-04-12 10:08:49
【问题描述】:

我想遍历数据库,对于每个值,我想通过在查询中使用 $idarray 中返回的 id 值来缩小搜索范围。例如,假设用户想要搜索“molly loves cats”,那么我想我首先在数据库中搜索“molly”并获取所有 id 并在下一次搜索时使用它们“爱”等等。最后,我会得到一个包含所有三个单词的所有 id 的列表。

搜索.php

$searchquery= (isset($_POST['searchq']) ? $_POST['searchq'] : NULL);
$q = explode(" ", $searchquery);
foreach($q as $key => $value){
        $in = implode("','", $idarray);
        if($stmt = mysqli_prepare($db_mysqli, "SELECT id FROM tbl_headlines WHERE (name LIKE ? OR header LIKE ?) AND id IN('$in')")){
            $searchquerypre = "{$value}%";              
            mysqli_stmt_bind_param($stmt, "ss", $searchquerypre);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_store_result($stmt);
            mysqli_stmt_bind_result($stmt, $id);

            if(mysqli_stmt_num_rows($stmt) > 0){
                while (mysqli_stmt_fetch($stmt)){
                    $idarray[] = array('id'=> $id, 'resultat'=> true);                                              
                }
            }
            if(mysqli_stmt_num_rows($stmt) == 0){
                $idarray[] = array('resultat'=> false);
            }
        }
    }
echo json_encode($idarray);

jquery

 $('#searchquery').on('keyup', function(){
            var searchquery = $("#searchquery").val();      

            if ($("#searchquery").val().length > 1) {
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: "search.php",
                    data: {searchq: searchquery},
                    success: function(result){
                        console.dir(result);

                    },
                    error: function(){
                        alert('Error');
                    }
                });
            }
        });

【问题讨论】:

  • 为什么可以在 1 个查询中完成?
  • 您可以在一个查询中搜索这些词,甚至可以设置该搜索的优先级
  • @Linda :所以你没有说你的代码到底出了什么问题??看起来你做的比用简单的方法做的困难多了..!

标签: php jquery mysqli


【解决方案1】:

从@scottevans93 回答中添加,您仍然可以采用相同的方式,但可能只是在您的 SQL 中合并搜索而不是硬编码循环 - 也将其设为对象,以便可以在任何地方使用。

class Search
{
    protected $SearchTerms;
    protected $Sql;

    public function __construct($terms)
    {
         $this->Sql = "SELECT * FROM Table WHERE :search";
         if(is_array($terms))? $this->SearchTerms = $terms : die();
         foreach ($this->SearchTerms as $_term)
         {
             $this->Sql .= " LIKE '%".$_term."%'";
         }
    }

    public function search($input)
    {
         $stmp = (Database:GetInstance()->Prepare($this->Sql))->execute(array(':search' => $input));
         return $stmp->fetchAll();
    }

}

可以这样使用:

$SearchUsers = new Search(array('Kyle','Bob','Jimmy'));
echo 'Users like your search: <br />';
foreach ($SearchUsers->search('Ky') as $_user)
{
    echo $_user . ', ';
}

此示例的输出将是“Kyle,”。 - 如果您有一个用户对象,您可以将其传递到搜索词中,然后更有效地使用search() 方法,这完全取决于您的层次结构/基础设施。

【讨论】:

    【解决方案2】:

    您可以通过多种不同的方式来处理您的问题,而无需使用 3 组不同的查询。

    一种可能的实现方式,假设您想以个人身份搜索单词,您可以使用LIKE ''

    SELECT * FROM `table` where `text` LIKE '%molly%' and `text` LIKE '%loves%' and `text` LIKE '%cats%';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 2017-02-27
      • 2018-09-11
      相关资源
      最近更新 更多