Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Search and Empty PHP


rocknbil - 3:06 am on Feb 8, 2012 (gmt 0)


It works but i want to the submit to search the whole db if nothing is selected to value of ""


Build your select dynamically. You'll have to use equality operators instead of between to make it more simple but the results are synonymous, plus you can expand it to any number of input values.

$where = null;


// 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)

Once you get your head around it, you can use it to build "or" clauses (wrap them in parentheses), order, and limit clauses from inputs.


Thread source:: http://www.webmasterworld.com/php/4415167.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com