Helminthophobe Posted January 5, 2008 Share Posted January 5, 2008 I have built a site that allows users to register an account. One of the options is to choose their gender. The gender is saved in a database with Male equaling 1 and Female equaling 0. I have a page later that allows the user to view their personal information. How do I make those ID numbers display the word Male or Female instead of displaying 1 or 0. Right now all I know how to do is the following: <tr> <td width="20%">Gender:</td> <td><? echo $row['gender']; ?></td> </tr> Any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/ Share on other sites More sharing options...
revraz Posted January 5, 2008 Share Posted January 5, 2008 Something like this should work <tr> <td width="20%">Gender:</td> <td><?php $sex=($row['gender']==1) ? "Male" : "Female"; echo $sex; ?></td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-430902 Share on other sites More sharing options...
Helminthophobe Posted January 5, 2008 Author Share Posted January 5, 2008 That worked perfect for Gender but I used this ID method on a couple areas and they have more than 2 options. Here is an example question I asked... What is your favorite fruit? Apple = 1 Pear = 2 Watermelon = 3 Other = 4 How would I translate those numbers into the proper word still using the same table code <tr> <td width="20%">Favorite Fruit:</td> <td><? echo $row['fruit']; ?></td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-430905 Share on other sites More sharing options...
emehrkay Posted January 5, 2008 Share Posted January 5, 2008 switch($row['fruit']){ case 1: echo 'apple'; break; case 2: echo 'pear'; break; } Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-430906 Share on other sites More sharing options...
Helminthophobe Posted January 5, 2008 Author Share Posted January 5, 2008 Awesome! Thanks for the quick replies! It's doing exactly what I wanted. Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-430909 Share on other sites More sharing options...
revraz Posted January 5, 2008 Share Posted January 5, 2008 You may want to have tables relating the id's to their values, and just do a join. Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-430913 Share on other sites More sharing options...
Helminthophobe Posted January 5, 2008 Author Share Posted January 5, 2008 How would I do that? Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-430947 Share on other sites More sharing options...
revraz Posted January 5, 2008 Share Posted January 5, 2008 Have a table like fruit fid | name 1 apple 2 pear Then when you do your query, you can do something like SELECT person.*, fruit.name FROM person JOIN fruit ON (person.fruitid = fruit.fid) WHERE person.id = $x Then in your Array or Object, you just echo that array index instead. Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-430957 Share on other sites More sharing options...
Helminthophobe Posted January 5, 2008 Author Share Posted January 5, 2008 I'm lost... I looked up JOIN on W3Schools and confused myself even more. From what I saw there though, I can set up my databases like this: users name fruit_id Bob 1 Fred 3 fruit fruit_id name 1 apple 2 pear 3 watermelon How would I display Fred's fruit preference? Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-431006 Share on other sites More sharing options...
revraz Posted January 5, 2008 Share Posted January 5, 2008 SELECT users.name, fruit.name FROM users JOIN fruit ON (users.fruit_id = fruit.fruit_id) WHERE users.id = 2 but your users table should be users id name fruit_id 1 Bob 1 2 Fred 3 Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-431010 Share on other sites More sharing options...
Helminthophobe Posted January 5, 2008 Author Share Posted January 5, 2008 Yea, I have IDs for the users. I just forgot to add it to my example. The code you posted would select watermelon for Fred, correct? How do I then display or echo that so it shows up on my site? Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-431017 Share on other sites More sharing options...
revraz Posted January 5, 2008 Share Posted January 5, 2008 The same way you do now, just do a print_r($row) to see your array and echo the appropriate index. Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-431020 Share on other sites More sharing options...
Helminthophobe Posted January 5, 2008 Author Share Posted January 5, 2008 Thanks for the extra help! It's much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-431026 Share on other sites More sharing options...
revraz Posted January 5, 2008 Share Posted January 5, 2008 No problem, its nice once you get the hang of joins, it really can clean up your code and put the data where it belongs, in a DB. Quote Link to comment https://forums.phpfreaks.com/topic/84567-solved-using-an-id-to-display-a-substitute-word/#findComment-431028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.