double0201 2014-06-07 07:59
浏览 71
已采纳

使搜索引擎忽略特定关键字

I'm currently using

    $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();



?>

this piece of code I learned from a tutorial to begin my search engine. I'll be honest, I don't know any PHP, and understanding what's going on here has been a real struggle.

How do I manipulate this piece of code such that if I search "Swarthmore College" or "Swarthmore", Swarthmore results will show up. I need to make sure that just typing "College" or "swar" or "a" won't trigger the Swarthmore results. But I need common words like "University", "of", and "Chicago" to trigger together for college searches like "University of Chicago." It should also be triggered by abbreviations like "UChicago."

I'm sorry if this is a lot to ask, but I am completely lost. Thank you in advance.

  • 写回答

1条回答 默认 最新

  • douwu8060 2014-06-07 08:30
    关注

    You should include an elseif clausule. The code would be:

    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>\"";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等