Page 1 of 1

If statement with multiple values

Posted: Tue Nov 01, 2005 6:24 am
by mhouldridge
Hi,

I have the following if statement which checks on other values from a list box;

Code: Select all

if ($list == "asset", "customer", "title", "oslicense") {	
	$result = mysql_query("SELECT * FROM dedicated WHERE $list LIKE '%$search%' ORDER BY asset");
	}
I get unexpected"," error.... any ideas?

Posted: Tue Nov 01, 2005 6:43 am
by mhouldridge
do I need a switch statement?

Posted: Tue Nov 01, 2005 7:27 am
by jmut
you might want to have a look at
http://php.net/in-array

Re: If statement with multiple values

Posted: Tue Nov 01, 2005 11:43 am
by BDKR
mhouldridge wrote:Hi,

I have the following if statement which checks on other values from a list box;

Code: Select all

if ($list == "asset", "customer", "title", "oslicense") {	
	$result = mysql_query("SELECT * FROM dedicated WHERE $list LIKE '%$search%' ORDER BY asset");
	}
I get unexpected"," error.... any ideas?
You can't compare multiple values at one shot like that. If you want to do it with one if statement, then it's better to do ...

Code: Select all

if ($list == "asset" || $list == "customer" || $list == "title" ||  $list == "oslicense") 
    {	/* do stuff  */  }
A switch would work here too, but the use of one or the other depends on a lot of other things I won't get into
here. Especially considering some of it may be a matter of opinion.

Now if what you really want is to check each of those items in the list (asset, customer, title, osilcense), then just put them in an
array and iterate.

Code: Select all

$list=array('asset', 'customer', 'title', 'osilcense')
foreach($list as $item)
    { /* do stuff with $item */ }
Cheers