Getting mysql data for a dropdown
Hello, I am sure this will have been covered many times, I have searched quite a bit but I am not sure how to word what it is I am looking for, sorry!
I have a database listing cats (Bill, Bob, Tabatha, etc).
Each cat's could have multiple behaviour traits (Sleepy, Active, Inquisitive, etc).
So as I don't want to have a behaviour column storing an array, I create another table with column names for each type of behaviour. Cat Bill gets a row in the new table with either 1 or 0 to identify whether he has each behaviour type.
However, on my page I want to create a dropdown which will show all used behaviour types but I have no idea how to do this. So:
Bob: Sleepy, Inquisitive
The drop down should show Sleepy and Inquisitive but not Active.
Any help would be very much appreciated!
Will the "behavior traits" ever change? For example, would you ever add, remove, or modify them? If so, I would suggest three tables: one for cats, one for behavior traits, and that relates the two. Each row in the "relation" table would consist of two elements - a candidate key from the cats table and one from the behavior table.
Otherwise, you'll want to build an array of all possible traits in PHP, and then you'd loop through that array to output the ones that have the "1" value in the row retrieved from your current SQL schema.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)