...

View Full Version : Generating all the switch case



curb
02-01-2007, 10:06 PM
Is there a way I can make a pull down menu generating a list off another file of all the destination case coming from the file locations.php?

locations.php



switch ($destination){
case "Las Vegas":
echo "Bring an extra $500";
break;
case "Amsterdam":
echo "Bring an open mind";
break;
case "Egypt":
echo "Bring 15 bottles of SPF 50 Sunscreen";
break;
case "Tokyo":
echo "Bring lots of money";
break;
case "Caribbean Islands":
echo "Bring a swimsuit";
break;
}


Thanks

GJay
02-02-2007, 09:18 AM
not really.
A better way would be to have something like


$destinations = array(
'Las Vegas'=>'Bring an extra $500',
'Amsterdam'=>'Bring an open mind'
//etc.
);

//and then your code above can be replaced with:
echo $destinations[$destination];

//and the select can be made with:
echo '<select name="destination">';
foreach($destinations as $destination=>$text) {
echo '<option value="'.$destination.'">'.$destination.'</option>';
}
echo '</select>';

curb
02-02-2007, 06:10 PM
Thanks. Is using the array better then switch case in general?

CFMaBiSmAd
02-02-2007, 06:57 PM
If you are just doing a 1 to 1 lookup/replacement, using an array would allow easy expansion, editing, or modification. If you need to do custom processing per entry, the switch/case statement would be the way to go.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum