PDA

View Full Version : PHP/MYSQL: Thoughts on categories?



benjaminj88
20 Aug 2009, 01:00 PM
Alright so I have a basic weblog I'm working on and when I wish to enter a new article or entry I have a form displayed. Now the issue at hand is what happens when I wish to enter more then one category for the entry? I currently have all the previously listed categories displayed with radio buttons, now I know I will need to change this to checkboxes to select more then one item. In turn however how do I store each category as it's own independent item so that When I go to the form it does not display:
Vehciles, day to day life, and art all under one check box but has each of them as their own item? Next question is how do I go about returning the checked items when editting the entry later on?

djlebarron
20 Aug 2009, 06:22 PM
You can save some work by giving the checkboxes the same name and putting empty brackets after the name like this: category []. What that will do is put a array (literally a string of category names that are seperated by commas) in your Db table's 'category' field.

To retrieve your entries by category type, do querys like this:

Select * FROM 'entries' WHERE 'category' LIKE '%vehicles%'
Select * FROM 'entries' WHERE 'category' LIKE '%life%'
Seclet * FROM 'entries' WHERE 'category' LIKE '%art%'

I would make your value for the "day to day life" checkbox 'life'. As long as you can recognize it in your code and you know what it means, you don't need to have the value be the same thing as what the user sees next to the checkbox.