Forum Moderators: coopster

Message Too Old, No Replies

checking two variables at once

in an if statement

         

jamie

10:37 am on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i am searching a database based on the choices from 3 pull down <select> menus.

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

andreasfriedrich

10:56 am on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

jamie

11:56 am on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thank you very much andreas - it works like a treat!

regards Jamie

Xuefer

1:03 pm on Apr 24, 2003 (gmt 0)

10+ Year Member



remember to quote the strings!

'all'
not -> all <-