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!

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>

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.

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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.