Jump to content

Archived

This topic is now archived and is closed to further replies.

barkster

Split Keyword Search Into Array?

Recommended Posts

I'm wanting to split my search keywords on my site into seperate words to increase likelyhood of hit.  Does anyone have an example on how to do this or maybe a script that will do something similar.  For instance if you search "car blue chevy" that it will return results for a "blue chevy car".

Thanks

Share this post


Link to post
Share on other sites
$key_words = explode(' ',$_GET['keywords']);

thats assuming you use the get method and the tet field in your form is called 'keywrods'

Share this post


Link to post
Share on other sites
Thanks, any suggestions on building the query string dynamically for the number of items. I know I can get the count of the array but was wondering when I loop through them what the best method for building my mysql query string.  Thanks

Share this post


Link to post
Share on other sites
try
[code]
<?php

$whereclause = '';
$where = array();
if ($_GET['keywords']) {
    $keywords = explode(' ', $_GET['keywords']);
    foreach ($keywords as $kw) {
        $kw = get_magic_quotes_gpc() ? trim($kw) : addslashes(trim($kw));
        $where[] = "(description LIKE '%$kw%')";
    }
    $whereclause = "\nWHERE " . join (" \nAND ", $where);
}
$sql = "SELECT id, description FROM mytable $whereclause";
echo '<pre>',$sql,'</pre>';
?>
<FORM>
<input type="text" name="keywords" size="40">
<input type="submit" name="submit" value="Search">
</FORM>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.