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