First, take a look at this page as an example: Click here
If you view the ‘Amenities’ section, there are 3 sets of checkboxes (Unit Features, Community Features and Utilities Included in Rent).
My Question Is: How can I use PHP to make 3 array variables (e.g. $unit_features, $community_features and $utilities_included) to store which boxes are/are not checked into 3 respective fields in a table?
More importantly: How do I pull the data out of the table in the same array format so that specifics can be viewed/edited/deleted?
- Note, I’ve tried countless times to do this by having separate fields in the table (as tinyints) – but it gets bulky and isn’t elegant at all…I even thought of making an object class but failed yet again..
You have a many-to-many relationship between properties and amenities. To model this, you need a separate table, not a variable number of columns.
There is one table which stores your properties.
There is one table which stores all possible amenities.
For each amenity a property has, insert one row into the table relating these two:
When you want to know what amenities a specific property has, just
SELECTall rows from this table for that property’s key. When you want to print out checkboxes for all amenities,SELECTfrom theamenitiestable and do aLEFT OUTER JOINto theproperty_amenitiestable. Those rows with null values from theproperty_amenitiestable are the boxes which are unchecked.Related reading. You should pick up a book on relational databases from your local BORDERS before they go out of business 🙂