MySQL-PHPage: Table record stuff

Status
Not open for further replies.
Hopefully this makes sense.

Let's say I have a table called Fruits with 3 fruits:
Code:
table Fruits
==============
1	Apple
2	Orange
3	Grape
At some point in HTML, I will convert that table into a selectable list that allows for multiple selections. So you could select Apple, or Apple + Grape.

When you submit this form, it inserts the data in a table. But I want it so that the IDs of the fruit are kept in one cell. So a table might look like this:
Code:
id	yourname	fruit
=============================
1	Me	1, 3
2	You	1, 2, 3
3	Him	2
Then I want to be able to take that "1, 2, 3" data in PHP, separate it, and display something like if ($table_row->fruit == 1) { echo "Apple." }.

I have no idea how to do this, or if there's a better way of creating the tables to begin with.
 
short answer: don't do it this way, use proper tables ;D

longer answer: Use the php explode() and implode() functions! Look them up on php.net. You can just store the list comma-separated when it submits and then when you load it, just explode the list if you need it into an array. Look them up :)

Edit:

Better tables should be (3 of them involved here):

tblFruit -> tblFruitUser -> tblUser

The user table is obvious, but say user 2 has fruits 1 and 3 - the FruitUser (called a "junction table" sometimes) would have two rows:

Code:
fkUserID, fkFruitID
2             1
2             3


Sorry if that's making no sense, I'm writing quickly!
 
Makes PERFECT sense. I knew the way I was going about it would require implode() and what not, it just didn't feel proper. But thanks for explaining the table. I should have thought of that. THANKS! :)
 
Boards of Canada said:
Makes PERFECT sense. I knew the way I was going about it would require implode() and what not, it just didn't feel proper. But thanks for explaining the table. I should have thought of that. THANKS! :)

No probs. Let us know if you ever need help on this shite again in the future ;). I've milked this forum for tons of info already ;)
 
Status
Not open for further replies.
Top Bottom