Hi, I am trying to build a search query which allows selection of multiple fields, saletype(hidden field), Make, Model, Keyword, Min Price, Max Price etc but I'm having a headache of a time trying to make it work the way I want.
Basically, if no items in the form are changed, I want the search to just pull in all rows from the db that have a saletype of n, but if the user selects a make, then for it to pull in all records for saletype = n AND Make = somemake etc.
If I use AND statements in the WHERE clause, and don't make any selections in the form, this returns 0 results unless all fields are selected, and if I use OR statements, then it doesn't return the desired results.
I've just came across the MATCH statement, but never used it before... is this what I should be using?
My code for the db is as follows:
Has anyone any idea on how I could make this work?
Many Thanks
Basically, if no items in the form are changed, I want the search to just pull in all rows from the db that have a saletype of n, but if the user selects a make, then for it to pull in all records for saletype = n AND Make = somemake etc.
If I use AND statements in the WHERE clause, and don't make any selections in the form, this returns 0 results unless all fields are selected, and if I use OR statements, then it doesn't return the desired results.
I've just came across the MATCH statement, but never used it before... is this what I should be using?
My code for the db is as follows:
Code:
$colname_salesRS = "-1";
if (isset($_POST['saletype'])) {
$colname_salesRS = (get_magic_quotes_gpc()) ? $_POST['saletype'] : addslashes($_POST['saletype']);
}
$colname2_salesRS = "-1";
if (isset($_POST['select'])) {
$colname2_salesRS = (get_magic_quotes_gpc()) ? $_POST['select'] : addslashes($_POST['select']);
}
$colname3_salesRS = "-1";
if (isset($_POST['select1'])) {
$colname3_salesRS = (get_magic_quotes_gpc()) ? $_POST['select1'] : addslashes($_POST['select1']);
}
mysql_select_db($database_dbname, $searchdb);
$query_salesRS = sprintf("SELECT aani_sales.sale_id, aani_sales.sale_make, aani_sales.sale_model, aani_sales.sale_customer, aani_sales.sale_year, aani_sales.sale_price, aani_sales.sale_title, aani_sales.sale_description, aani_sales.sale_mileage, aani_sales.sale_body, aani_sales.sale_fuel, aani_sales.sale_gearbox, aani_sales.sale_engine, aani_sales.sale_location, car_makes.car_make, car_models.car_model, aani_sale_pics.pic_image FROM aani_sales LEFT JOIN car_makes ON aani_sales.sale_make = car_makes.car_id LEFT JOIN car_models ON aani_sales.sale_model = car_models.car_mod_id LEFT JOIN aani_sale_pics ON aani_sales.sale_id = aani_sale_pics.sale_id WHERE aani_sales.sale_type = %s AND aani_sales.sale_make = %s AND aani_sales.sale_model = %s GROUP BY aani_sales.sale_id ORDER BY aani_sales.sale_price DESC", GetSQLValueString($colname_salesRS, "int"),GetSQLValueString($colname2_salesRS, "text"),GetSQLValueString($colname3_salesRS, "text"));
$salesRS = mysql_query($query_salesRS, $autoadsni) or die(mysql_error());
$row_salesRS = mysql_fetch_assoc($salesRS);
$totalRows_salesRS = mysql_num_rows($salesRS);
Has anyone any idea on how I could make this work?
Many Thanks