尝试实现多个关键字搜索但无法获得所需的结果

时间:2014-05-14 22:39:30

标签: 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."%'";
}

1 个答案:

答案 0 :(得分:0)

我刚刚改变了你的代码,看看......

$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作为一个数组,但我没有改变......