Jump to content

How to make search form retrieve results no matter what order of keywords?


negationst8

Recommended Posts

Hi,

 

I am using a software which allows users to post advertisements.  Unfortunately, the search function of the software isn't very sophisticated.

 

For example, if there is an advertisement for a "black car with six gears" and a user performs a search on "black gears" the ad will appear in the search results.  However, if a user performs a search on "gears black" the ad will not appear.

 

I want the ad to be retrieved no matter what order a user types in the keywords.

 

The code I need to manipulate is this:

 

if(trim(preg_replace("/[\s,]+/", '', $text_search)) != '') {

$keywords = preg_split("/[\s,]+/", $text_search);

$keywords = array_unique($keywords);

$search .= ' AND (';

$prefix = '';

for($i = 0; $i < count($keywords); $i++) {

$search .= $prefix."a.ad_text LIKE '%".$keywords[$i]."%' ";

$prefix = 'OR ';

}

$search .= ') AND a.published = 1';

} else {

$search .= ' AND a.published = 1';

}

 

 

Thank you for all your help.

 

James

I would strongly suggest that you have a fulltext index on your keywords field and then use MATCH... AGAINST ...  sql to perform the search

 

see http://dev.mysql.com/doc/refman/5.1/en/fulltext-fine-tuning.html

Archived

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

×
×
  • Create New...

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.