【问题标题】:Make the search engine ignore specific keywords让搜索引擎忽略特定的关键字
【发布时间】:2014-06-07 07:59:31
【问题描述】:

我目前正在使用

    $terms = explode(" ", $k);
    $query = "SELECT * FROM search WHERE ";


    foreach ($terms as $each) {
        $i++;

        if ($i == 1)
            $query .= "keywords = '%$each%' ";
        else
            $query .= "OR keywords LIKE '%$each%' ";
}

    // connect
    mysql_connect("localhost", "root", "password");
    mysql_select_db("search");

    $query = mysql_query($query);
    $numrows = mysql_num_rows ($query);
    if ($numrows > 0) {

        while ($row = mysql_fetch_assoc($query)) {
            $id = $row['id'];
            $title = $row['title'];
            $description = $row['description'];
            $keywords = $row['keywords'];
            $links = $row['link'];

            echo "<h2>$title</h2><p>$description</p>";

        }

    }
    else
        echo "No Search Results have been found for \"<b>$k</b>\"";

    // disconnect
    mysql_close();



?>

我从教程中学到的这段代码开始了我的搜索引擎。老实说,我不知道任何 PHP,并且理解这里发生的事情是一场真正的斗争。

如何操作这段代码,如果我搜索“Swarthmore College”或“Swarthmore”,就会显示 Swarthmore 的结果。我需要确保仅键入“College”或“swar”或“a”不会触发 Swarthmore 结果。 但是我需要像“University”、“of”和“Chicago”这样的常用词来共同触发像“University of Chicago”这样的大学搜索。它也应该由诸如“UChicago”之类的缩写触发。

很抱歉,如果这有很多问题要问,但我完全迷路了。 提前谢谢你。

【问题讨论】:

  • 你的解释有点混乱。在哪些情况下您希望触发更多结果?它取决于什么?
  • 我希望在关键字足够具体以仅识别一所大学时触发该特定大学的结果。因此,如果我的数据库中有“特拉华大学”和“芝加哥大学”,我希望所有 3 个关键字都是显示结果所必需的。

标签: php search keyword


【解决方案1】:

您应该包含一个elseif 分句。代码将是:

if ($numrows == 1) {
    // You dont need to iterate if you want to show only unique result
    $row = mysql_fetch_assoc($query);
    $id = $row['id'];
    $title = $row['title'];
    $description = $row['description'];
    $keywords = $row['keywords'];
    $links = $row['link'];

    echo "<h2>$title</h2><p>$description</p>";
}
elseif ($numrows > 1)
    echo "More than one Search Results have been found for \"<b>$k</b>\". Please be more specific ";
else
    echo "No Search Results have been found for \"<b>$k</b>\"";

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2011-07-24
    相关资源
    最近更新 更多