php - 尝试实现多个关键词搜索但无法得到想要的结果

标签 php mysql arrays

我正在尝试在我的应用程序中实现多个关键字搜索,但它仅搜索输入中的最后一个单词并忽略输入的其余部分有人可以帮助我解决问题吗,下面是我的代码...

$where_clause=array();
               if(!empty($vResume_screen))
    {
    // trim whitespace from the stored variable
               $trimmed = trim($vResume_screen);  
    // separate key-phrases into keywords
               $trimmed_array = explode(" ",$trimmed);
    // count keywords
               $trimm_total = count($trimmed_array);
                $i = 0;
                 $searchstring = '';
    // looping to get the search string
               foreach ($trimmed_array as $trimm)
    { 
        if ($i != 0 and $i != $wordcount)
        { 
            $searchstring .= " AND ";
        } 
        $searchstring .= "resume_text LIKE '%$trimm%'"; 

        // incrementing the value 
        $i = $i + 1; 
    }
         $where_clause[]="resume_text like '%".$searchstring."%'";
}

最佳答案

我只是稍微改变了你的代码,看看......

$where_clause=array();
if(!empty($vResume_screen))
    { 
        // separate key-phrases into keywords
        $trimmed_array = explode(" ", trim($trimmed));

        // count keywords
        $trimm_total = count($trimmed_array);

        $searchstring = '';

        // looping to get the search string
        foreach ($trimmed_array as $trimm)
        { 
           if (!empty($searchstring)) { 
               $searchstring .= " OR "; //If you want to put the same condition for different keywords, you need to use OR.
           } 
           $searchstring .= " resume_text LIKE '%" . $trimm . "%' "; 
    }
    array_push($where_clause, $searchstring);
}
  • 我不知道为什么你需要 $where_clause 是一个数组,但我没有改变......

关于php - 尝试实现多个关键词搜索但无法得到想要的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23666347/

相关文章:

php - 我应该迁移到mysql吗?

arrays - Kotlin中数据类的equals方法

php - 基思·伍兹倒计时器 DATETIME 格式 Jquery

php - 查找数组值之间步数的有效方法

Mysql group_concat() 重新排序结果集

mysql - Laravel 5.6 如何通过 id 获取数据并打印 10 by 10

c++ - 指针数组栈实现

ruby - 重现随机数组排序

PHP 循环嵌套 JSON 响应并重新组装为 Webhook 的简单查询字符串

php - 查看一个字符串是否包含另一个字符串