I’m having trouble figuring out the best way to store what information my users want to see. I apologize for not being able to find an easy way to explain my problem, but here goes (the fruits are for example’s sake):
Let’s say my users have an option to keep track of the following fruits in their fridge:
Apples, Oranges, Grapes, Pears
The database is already setup with a table called ‘fruits’, where Apples is id_fruit=1, Oranges is id_fruit=2, etc.
Like I said all users have the OPTION to keep track of these fruits, however… I currently can not figure out an easy way to store the users selection of WHICH fruits they want to track. If user 1 doesn’t like Apples, he can just go into his settings, uncheck Apples and no longer have Apples show up in his dashboard.
The only way I can think to store this information is to create a table that contains for fields user_id, apples, orange, grapes, pears. And then storing the user_id and Yes/No or 0/1 for the fruits.
This causes major grief if I ever want to add more fruits, or whatever.
What is the best way to store this information in MySQL, using PHP. And how would I retrieve that info.
User logs in, check the DB for which fruits he wants to see, and display only those fruits he has chosen.
Any help would be greatly appreciated.
This is a classic has-and-belongs-to-many, or simply many-to-many, relationship.
In
users_fruitsyou store one row per user-fruit relationship:(user_id: 42, fruit_id: 3).