Forum Moderators: coopster
define(POTATO,1);
define(TOMATO,2);
What's the best way to go the other way where I am getting the constants as numbers (In this case 1 and 2) and I want to know the name of it?
Is making an array like
$food_array = {POTATOE => 'Potato',TOMATO => 'Tomato'}
the best solution or is there a better way of handling this?
$food_array = array()
array_push($food_array, array('POTATO', '1', 'Potato'));
array_push($food_array, array('TOMATO', '2', 'Ketchup'));
print_r($food_array);
Although, I don't really understand your point since define(POTATO) will always be equal to string 'Potato'.
What's the best way to go the other way where I am getting the constants as numbers (In this case 1 and 2) and I want to know the name of it?
I don't think it will be very logical to try to find the constant to from the number it is assigned to. Why would you want to do that, you might use conditions (if, switch..) to achive the same thing.
I am not sure what you are trying to do with the array neither.
Whatever you can do with the array you mentioned can be done by playing around the following:
$food_array = array('Potato', 'Tomato');
Then, usually, in a database, there is an ID field which is unique.
ID / Food_name
1 / Tomato
2 / Potato
3 / Fruits, etc...
And the number used in your radio choice will be the ID number returned by your database.
YOUR QUERY
**********
$food_array=array();
$sql = "select id, food_name_eng from DB order by food_name ASC";
$res=mysql_query($sql) or die ("Fail to get food");
while ($pres=mysql_fetch_row($res)) {
array_push($food_array, $pres[0]);
}
print_r($food_array); YOUR FORM
*********
echo '<select size="1" name="food" id="food">
<option value="nothing">Choose food...</option>
<option value="nothing">---------</option>';
for($i="0"; $i<count($food_array); $i++) {
echo '<option value="'.$food_array[$i][0].'" ';
if(isset($_POST['food']) AND $food_array[$i][0]==$_POST['food']) {echo ' selected';}
echo '>'.ucfirst($food_array[$i][1]).'</option>';}
echo '</select>'; Hope you understand the above.
Tomda
[edited by: tomda at 1:05 pm (utc) on April 28, 2008]