Jump to content

[SOLVED] Using an ID # to Display a Substitute Word


Helminthophobe

Recommended Posts

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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.