Forum Moderators: coopster
It works but i want to the submit to search the whole db if nothing is selected to value of ""
// Keys on left, table fields and comparison operators on right
$vars = array(
'make' => array ('car_Make','='),
'pf' => array('car_price','>='),
'pt' => array ('car_price','<=')
);
//
foreach ($vars as $key => $op_array) {
if (isset($_POST[$key]) and ! empty($_POST[$key])) {
// Only need to add "and" if $where has been started.
// Note the spaces. Important.
if ($where) { $where .= ' and'; }
// Wrap text values, not needed for numeric values
$tmp = ($key=='make')?"'".$_POST[$key]."'":$_POST[$key];
// Ex: " car_price>=2000 " or " and car_price>=2000 "
$where .= " " . $op_array[0] . $op_array[1] . $tmp;
}
}
//
$query = "select * from site_cars";
if ($where) { $query .= " where $where"; }
This can give you
select * from site_cars (nothing selected, all records)
select * from site_cars where make='Chevy'; (make only)
select * from site_cars where make='Chevy' and car_price>=2000 (greater than only)
select * from site_cars where make='Chevy' and car_price<=5000 (less than only)
select * from site_cars where make='Chevy' and car_price>=2000 and car_price<=5000 (all inputs)