简体   繁体   中英

Make the search engine ignore specific keywords

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.

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>\"";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM