0

我的搜索有效,但如果使用了很多单词,它会返回大量不相关的东西。任何关于如何将搜索固定到哪里的想法,如果搜索长短语,它会拉出一个更窄的结果,而不是更多的东西。我认为这个功能可能正在为每个单独的文章标题搜索每个关键字,我不确定它有什么问题

function search_results($search_for) {
$returned_results = array();
$where = "";

$search_for = preg_split('/[\s]+/', $search_for);
$total_search_words = count($search_for);

foreach($search_for as $key=>$search_term) {
    $where .= "`review_title` LIKE '%$search_term%'";

    if($key != ($total_search_words - 1)) {
        $where .= " OR ";
    }
}
$value = $_POST['search_value'];
$results = "SELECT * FROM `reviews` WHERE `review_referral` = '$value' AND $where";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;

if($results_num === 0) {
    return false;
} else {

    while($results_row = mysql_fetch_assoc($results)) {
        $returned_results[] = array(
                            'review_title' => $results_row['review_title'],
                            'review_body' => $results_row['review_body'],
                            'user_id' => $results_row['user_id'],
                            'user_firstname' => $results_row['user_firstname'],
                            'user_lastname' => $results_row['user_lastname'],
                            'review_id' => $results_row['review_id'],
                            'review_body' => $results_row['review_body'],
                            'review_referral' => $results_row['review_referral'],
                            'rating' => $results_row['rating'],
                            'review_date' => $results_row['review_date'],
                            'review_category' => $results_row['review_category'],
                            'review_url' => $results_row['review_url'],
                            'referral_category' => $results_row['referral_category'],
        );

    }

return $returned_results;
4

1 回答 1

0

如果您想缩小搜索范围,您应该使用 AND 而不是 OR。

if($key != ($total_search_words - 1)) {
    $where .= " AND ";
}
于 2012-06-24T02:05:57.243 回答