Multi-criteria query, the null value submitted (condition) how to handle
Table 1 has brands, years, models
Board (Bmw,ford,aodi)
Year (2011,2012,2013)
Type (CAR,SUV,MPV)
I made three drop-down menus, and the user selects the query
1, selected BMW in board and 2013 in year, but the third type was not chosen,
But when you do, the Type field is sent empty, and the query is equal to
Board= ' BMW ' and year= ' and type= '
I want to implement the not selected fields are NOT NULL are all models of BMW and 2013,
Board= ' BMW ' and year= "+" and type is not NULL
Or
Board= ' BMW ' and year= "2013"
How to do this,
Share to:
------Solution--------------------
You can refer to the wording
$where = Array ();
if ($board) {
$where []= "board= ' $board '";
}
if ($year) {
$where []= "year= ' $year '";
}
....
$sql. = (!empry ($where))? ' WHERE '. Implode (' and ', $where): ';
------Solution--------------------
After submission, $_post is
$_post = Array (' board ' = ' BMW ', ' year ' = ' + ', ' type ' = ' = ');
The
$w = Array ();
foreach ($_post as $k = = $v) {
if (! empty ($v)) $w [] = "$k = ' $v '";
}
$where = Join (' and ', $w);
Get
Board= ' BMW ' and year= ' 2013 '