I am trying to create a search function that will sort results by relevance based upon not only matches but also exclusions. I've got the matching part down good and have tested it out, but it's the exclusions I'm not sure about.
First of all, here is a very condensed version of a sample search form which includes the items needing for match and the exclusion element:
Once the form is submitted, the exclusions are inserted into a PHP variable $exclusions and separated with commas like this:HTML Code:Select Year: <select name="year"> <option value="1995">1995</option> <option value="1996">1996</option> <option value="1997">1997</option> </select> Select Color: <select name="color"> <option value="blue">Blue</option> <option value="red">Red</option> <option value="yellow">Yellow</option> </select> Sizes To Exclude: <input type="checkbox" name="sizes[]" value="x-small">X-Small <input type="checkbox" name="sizes[]" value="small">Small <input type="checkbox" name="sizes[]" value="large">Large <input type="checkbox" name="sizes[]" value="x-large">X-Large
Then the database will have the fields of year, color, sizes. In the sizes field, there will be values in the same format as the $exclusions variable: x-small, large, x-largePHP Code:$exclusions = "x-small, large";
My initial search for matches is as follows:
$query = mysql_query("SELECT id, if(year='$year',1,0) + if(color='$color',1,0) as score FROM company ORDER BY score DESC");
My obvious problem is I don't know how to influence this search with the exclusion values. Anyone have any ideas?


Reply With Quote

Bookmarks