Hi,
On the whole, your search follows the correct general ideas, since it seems to search the database on the basis of WHERE year LIKE '%$q%' OR make LIKE '%$q%' OR model LIKE '%$q%' OR part_name LIKE '%$q%'...
The inclusion of a combined free text query, where your programme, on receiving input, must parse the string in accordance with some predefined rules for search string format, really depends on how many different formats you are willing to cater for. You could build up an array of plausible input types and try to catch each one out, but this is inevitably going to be difficult and offers the possibility for items slipping through the search. After all, your aim is really to get car parts sold, isn't it?
A simpler option might be to impose more structure over the method of input. Have things like the year as a drop down list of reasonable options. Perhaps have the Make and Model presented similarly (and, while you're at it, turn your "make" and "model" fields, in your database, into types such as enum('Acura', 'Honda', 'Subaru' ... etc.) while you're at it, to help enforce some uniformity over your data - no point having a 'Subaru Impreza left front fender' sitting on the shelf gathering dust, simply because someone spelt it 'Impretza' when putting it in the database, so it never turns up in searches, is there ;).
Then you simply pull out your search values as $year=$_GET['year'], $make=$_GET['make'] and $q=$_GET['q'] and run a query like
SELECT <field list> FROM inventory WHERE year_from>='$year' OR year_to<='$year' AND Make='$make' AND part_name LIKE '%$q%'
Furthermore, you could cater for the 'any' option of Make and Year, by having the first element of each dropdown as <option value="">-Any-</option>. Build your query string over several lines and test for the "Any" options for year and make thus:
$query = "SELECT <field list, whatever> FROM inventroy WHERE ";
if($_GET['year']!=''){
$query .= "year_from>='{$_GET['year']} AND year_to<='{$_GET'year']}' AND ";
}
if($_GET['make']!=''){
$query .= "AND make='{$_GET['make']}' AND ";
}
$query .= "part_name LIKE '%$q%' AND other_fields LIKE '%$q%' AND so_on LIKE '%$q%' ORDER BY make";
Anyway, that's probably how I'd do it. Your aim is to sell car parts, like I say, so make your search form as explicit as possible about how the customer is to get to see the parts they want. After all, if they're just stuffed their Subaru Impreza into a hedge, they won't mind foregoing a little ease of input for the chance to get their hands on that new fender, are they?
Let us know how you get one.
Take it easy,
Dan
|