php代码没有按照我想要的搜索功能的方式工作

时间:2011-11-15 17:28:44

标签: php search foreach

搜索一个单词非常有效。我想有两个或更多的单词也可以。

到目前为止,我已经做到了这一点,如果我搜索多个单词,结果就是这样:

SELECT id, title, tag, type 
FROM table WHERE **AND** p.id LIKE '%flower%' OR title LIKE '%flower%' OR tag LIKE '%flower%' OR type LIKE '%flower%' 
AND p.id LIKE '%floral%' OR title LIKE '%floral%' OR tag LIKE '%floral%' OR type LIKE '%floral%' 
ORDER BY title

如何正确使用 AND ,以便在有多个单词搜索时,我的代码中没有额外的AND。

有更有效的方法吗?

if (count($error) < 1) {
  $searchSQL = "SELECT id, title, tag, type     
                FROM table
                WHERE ";

 $searcheach = explode(" ", $searchTerms);

foreach($searcheach as $searchword) {

//if more than one word do this else do that
 if(strpos(trim($searchTerms), ' ') !== false) {
     $searchSQL .= "AND p.id LIKE '%{$searchword}%' 
                OR title LIKE '%{$searchword}%'
                OR tag LIKE '%{$searchword}%'
                OR type LIKE '%{$searchword}%' ";
     } 
     else {
         $searchSQL .= "p.id LIKE '%{$searchword}%' 
                OR title LIKE '%{$searchword}%'
                OR tag LIKE '%{$searchword}%'
                OR type LIKE '%{$searchword}%' ";
         }  

}
$searchSQL .= "ORDER BY title";

4 个答案:

答案 0 :(得分:4)

$words = explode(' ', $searchTerms);

$clauses = array()
foreach($words as $word) {
    $safeword = mysql_real_escape_string($word);
    $clauses[] = "(p.id LIKE '%{$safeword}%' OR title LIKE '%{$safeword} OR etc... )";
}
$clause = implode(' AND ', $clauses);

$sql = "SELECT ... WHERE $clause ORDER BY title";

答案 1 :(得分:0)

我宁愿做

$searchSQL = "SELECT id, title, tag, type     
            FROM table
            WHERE (1=1)";

现在,您可以在每个条件

之前始终添加AND

此外,看起来你错过了括号,我相信它应该是

$searchSQL .= "AND (p.id LIKE '%{$searchword}%' 
            OR title LIKE '%{$searchword}%'
            OR tag LIKE '%{$searchword}%'
            OR type LIKE '%{$searchword}%') "

答案 2 :(得分:0)

我通常使用1 = 1过滤器,这样当搜索多个单词时,sql将是:

SELECT id, title, tag, type 
  FROM table 
  WHERE 1=1 
    AND p.id LIKE '%flower%' 
    OR title LIKE '%flower%' 
    OR tag LIKE '%flower%' 
    OR type LIKE '%flower%' 
    AND p.id LIKE '%floral%' 
    OR title LIKE '%floral%' 
    OR tag LIKE '%floral%' 
    OR type LIKE '%floral%' 
  ORDER BY title

这样做的代码是:

if (count($error) < 1) {
  $searchSQL = "SELECT id, title, tag, type     
                FROM table
                WHERE 1=1";

 $searcheach = explode(" ", $searchTerms);

foreach($searcheach as $searchword) {

//if more than one word do this else do that
 if(strpos(trim($searchTerms), ' ') !== false) {
     $searchSQL .= " AND p.id LIKE '%{$searchword}%' 
                OR title LIKE '%{$searchword}%'
                OR tag LIKE '%{$searchword}%'
                OR type LIKE '%{$searchword}%' ";
     } 
     else {
         $searchSQL .= "p.id LIKE '%{$searchword}%' 
                OR title LIKE '%{$searchword}%'
                OR tag LIKE '%{$searchword}%'
                OR type LIKE '%{$searchword}%' ";
         }  

}
$searchSQL .= "ORDER BY title";

答案 3 :(得分:0)

我倾向于将所有术语构建为数组,然后使用implode:

foreach($searcheach as $searchword) {
    $sqlSearchTerms[] = "p.id LIKE '%{$searchword}%' 
     OR title LIKE '%{$searchword}%'
     OR tag LIKE '%{$searchword}%'
     OR type LIKE '%{$searchword}%' ";
}

$searchSQL .= implode(' AND ', $sqlSearchTerms);