Forum Moderators: coopster
<select name="Gender" size="1" title="Gender">
<option value="<?php echo $Gender?>"><?php echo $Gender?></option>
<option value="Female">Female</option>
<option value="Male">Male</option>
</select>
I like to use numbers, this way the db will be smaller and faster I believe, but how do I display the results?
See below:
<select name="Gender" size="1" title="Gender">
<option value="<?php echo $Gender?>"><?php echo $Gender?></option>
<option value="1">Female</option>
<option value="2">Male</option>
</select>
I am sure there must be another trick for that isn't there?
That's a good idea. I'd treat it as a boolean value either 0 (female) or 1 (male). That way when you want to display it all you need is this...
if ($gender)
echo "Male";
else
echo "Female";
Using the tenary operator [us3.php.net] you can simplify it down to this...
echo ($gender) ? "Male" : "Female";
Tim
I like to use numbers, this way the db will be smaller and faster I believe, but how do I display the results?
there are many ways how you can write the mapping of number to string in your code. since the number can only have 2 values and you're outputting from your database maybe multiple times, i would suggest you using an array. this is maybe faster then an if-block:
$GenderCaps = array(1 => "male", 2 => "female");
echo($GenderCaps[$GenderVal]); you only need to define the array once.
I do understand Timotheos and hakre solution to understand Nutter I will have to find out more about enum data types.
It seams to me that you all think that this is the only object I have. Is there another solution if I have more options?
This may not be what you're looking for, but you might also look at the set data type. Similar to enum in that you enter a list of valid values, but the set type allows you to pick more than one.
hakre's method will work for states and stuff with more than two options.
- Ryan