Forum Moderators: coopster

Message Too Old, No Replies

Reversing constants to a name?

         

NooK

10:28 am on Apr 28, 2008 (gmt 0)

10+ Year Member



If one has something like

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?

tomda

10:33 am on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep ! Array is the best solution.

$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'.

Habtom

10:33 am on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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');

NooK

12:07 pm on Apr 28, 2008 (gmt 0)

10+ Year Member



Well imagine a database storing user information and each user having a favorite food. When storing on the database and alos making radio choices it is best to use the numbers (Also makes easier for comparison and such), thus the "defines" but when pulling the data from the database I want the user to see the food name and not just a number so when pulling from the database I need a way to name that Tomato (Which will just be a returned number).

tomda

12:17 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I get it...

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]

NooK

12:44 pm on Apr 28, 2008 (gmt 0)

10+ Year Member



Yeah, I got it, except in my case the food are actually status of a case (Something like, started, approved, active and such) thus not worth having a DB table to store just those values, thus while the codes will be in the database there's no table making a distinction of each status code to a string. I guess that leaves me the choice to either put them in the DB (Even if it is just 7 or 8 status codes which will never be changed) or have them hardcoded in a array in the code itself.

tomda

12:50 pm on Apr 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then, a nice master array like above will do the trick...

Tomda

NooK

12:56 pm on Apr 28, 2008 (gmt 0)

10+ Year Member



Thanks