[thelist] Dynamic if statement in PHP

Bill Haenel mail at webmarketingworx.com
Thu Oct 24 07:29:01 CDT 2002


> I have a query string that might have several variables, say from one to
> five, not always the same variables, and not always the same number of
> variables...I've managed to put the variable names and values in an array,
and I'm
> able to generate the conditions depending on the variables I have.

How 'bout:

In the form...
<input type="text" name="fruit[apple]" value="Red">
<input type="text" name="fruit[banana]" value="Yellow">
<input type="text" name="fruit[tangerine]" value="Green">

In the script...
<?
// set the good fruit colors
$ripe_fruits = array (
	'apple' => 'Red',
	'banana' => 'Yellow',
	'tangerine' => 'Orange'
)

// check the fruit from the form against the good fruit colors
foreach ($fruit as $key => $value) {
	if ($value == $ripe_fruits[$key]) $ripe++;
}

// report whether all the fruits are ripe or not
$good_fruit = ($ripe == count($fruit));
?>

You could just as easily put it in a function that deals with the names and
values generically.

BH




More information about the thelist mailing list