Forum Moderators: open
I'm trying to create a Search facility for a database using a form with checkboxes. I have set up the query so that the variables can be returned empty, ie. Manufacturer, Model and Price from to price to but I also need to query a list of checkboxes of the accessories wanted with the vehicle.
The first query works perfect, but when you add the checkbox subquery, nothing happens if the checkboxes are left empty and just search for a Ford. Then, if you leave the manufacturer, model and prices boxes empty and just search for vehicles with specific accessories, the array created from the boxes checked only returns the vehicles with the first checked item, ignoring the other checked boxes.
What I need is a search where you can either complete all fields, or just one. Then, when the query is executed, all fields that have been completed are searched for and only results with all of the fields are returned.
Does that make sense?
Anyway, here is the code I've got so far:
<?
$acc_array = array($accessories_1, $accessories_2, $accessories_3, $accessories_4, $accessories_5, $accessories_6, $accessories_7, $accessories_8, $accessories_9, $accessories_10, $accessories_11, $accessories_12, $accessories_13, $accessories_14, $accessories_15, $accessories_16, $accessories_17, $accessories_18);
$accessories = implode(" ", $acc_array);$result2 = mysql_query("SELECT DISTINCT vehicles.id, vehicles.manufacturer_id, vehicles.model, vehicles.engine, vehicles.price, vehicles.special, vehicles.special_price, vehicles.description, manufacturers.name FROM vehicles, manufacturers, car_accessories, accessories WHERE vehicles.manufacturer_id LIKE '%$manufacturer%' AND manufacturers.id = vehicles.manufacturer_id AND vehicles.price BETWEEN '$price_from' AND '$price_to' AND vehicles.model LIKE '%$model%' AND vehicles.id IN (SELECT car_accessories.car_id FROM car_accessories WHERE car_accessories.accessory_id HAVING ('$accessories')) ORDER BY vehicles.price DESC LIMIT $from, $max_results") or die (mysql_error());
?>
Can anyone see how to optimize this code to work with all of the variables?
Any help would be great, thanks.
What I need is a search where you can either complete all fields, or just one. Then, when the query is executed, all fields that have been completed are searched for and only results with all of the fields are returned.
You said it right there. You check each field submitted for a value and if it exists and is populated correctly you condition it to be used in your query and append (concatenate) to your initialized query statement.