通过PDO PHP

时间:2016-10-13 09:29:22

标签: php mysql sql pdo keyword

我想知道是否有一种方法可以通过关键字匹配来过滤结果,只有最多可以显示2个关键字才能显示结果?

很快......我想通过使用关键字($ row ['TAG'])来做“你可能感兴趣”...所有关键字相似的唯一条件或最多2个字的差异。 TIA。

$words_tag=apple, 11128gb, mjvm2ll, air, macbook, 16ghz, i5250u,
notebook, laptop, mac, **- 10 keyword**
  

V $ words_tag(2)= apple,air,macbook,16ghz,i5250u,notebook,   笔记本电脑,mac, -8/10关键字

     

V $ words_tag(3)= mjvm2ll,air,macbook,16ghz,i5250u,notebook,laptop,mac, -8/10关键字

     

X $ words_tag(4)= mjvm2ll,air,16ghz,i5250u,笔记本电脑,笔记本电脑,mac,    -7/10关键字

我的代码:

    $words_tag=$row['TAG'];
$arr = substr_count($words_tag, ",");

    //$words_tag=apple, 11128gb, mjvm2ll, air, macbook, 16ghz, i5250u, notebook, laptop, mac,

    $search = $words_tag; 
    $search = strtoupper($search);
    $search = strip_tags($search); 
    $search = trim($search);
    $words = explode(',', $search);
    $words_condition = array();
    $arguments = array();
    $i='1';
    foreach ($words as $word) {
        $words_condition[] = 'TAG LIKE ?';
        $arguments[] = '%'.$word.'%';
        $i++;
        if($i=> $arr-2) break();
    }

    $query = $db->prepare("SELECT * FROM `post` WHERE ".implode(" OR ", $words_condition)." ORDER BY ID DESC");

    $query->execute($arguments);   

    if($query->rowCount()=='0'){

    }else{
    echo 'YOU MAY BE INTSETENTING...';
    }

1 个答案:

答案 0 :(得分:2)

使用OR而不是LIKE组合+条件。如果匹配,则条件为1;如果不匹配,则条件为0,因此总数将是匹配的关键字数。然后,您可以将其与匹配关键字数量的限制进行比较。

$query = $db->prepare("SELECT * FROM `post` 
    WHERE ".implode(" + ", $words_condition)." BETWEEN 1 AND 2 
    ORDER BY ID DESC");