Forum Moderators: coopster
it works perfectly if all three choices are made, but if i allow an "all" in more than one of the <selects> - i can't do the mysql_db_query properly.
here are the pull down menus:
<select name="a">
<option value="all" selected> All </option>
<option value="Location 1"> Location 1 </option>
<option value="Location 2"> Location 2 </option>
<select name="b">
<option value="all" selected> All </option>
<option value="Category 1"> Category 1 </option>
<option value="Category 2"> Category 2 </option>
<select name="c">
<option value="Type 1" selected> Type 1 </option>
<option value="Type 2"> Type 2 </option>
here is the code i have so far:
$var = @$_GET['a'] ;
$var2 = @$_GET['b'] ;
$var3 = @$_GET['c'] ;
if ($var == all)
$query = "select * from my_table where (category=\"$var2\") and (type=\"$var3\") order by ID desc, name";
elseif ($var2 == all)
$query = "select * from my_table where (location=\"$var\") and (type=\"$var3\") order by ID desc, name";
elseif ($var == all && $var2 ==all)
$query = "select * from table where (type=\"$var3\") order by ID desc, name";
else
$query = "select * from my_table where (location=\"$var\") and (category=\"$var2\") and (type=\"$var3\")
order by ID desc, name";
the blue line is where i want to check the values of &var and &var2 and set variable &query accordingly, but no results are returned?
appreciate any help
many thanks
elseif ($var == all && $var2 == all) will never get evaluated since if ($var == all) will always already be true for any cases that elseif ($var == all && $var2 ==all) itself might be true. You need to test for the most specific conditions first. The string all should be written as 'all'. It works without the quotes since PHP [php.net] will treat bare words as strings if they are no reserved words. But it is always better to explicitly declare your strings.
Andreas