Jump to content


Photo

Split Keyword Search Into Array?


  • Please log in to reply
4 replies to this topic

#1 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 01 September 2006 - 02:46 PM

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

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 01 September 2006 - 02:57 PM

$key_words = explode(' ',$_GET['keywords']);

thats assuming you use the get method and the tet field in your form is called 'keywrods'
follow me on twitter @PHPsycho

#3 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 02 September 2006 - 12:42 AM

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

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 02 September 2006 - 12:30 PM

try
<?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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 02 September 2006 - 08:34 PM

Cool I'll try it out, thanks for the help




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users