![]() |
Category System Tutorial
When I first started using PHP, and really still today, I used it for Content Management Systems. I got them to be pretty advanced, except there was one thing that always got me. Whenever I did categories, I was limited to however many fields I had in the "articles" table. I'm sure you PHP gurus already know how I'm going to solve this problem, so this article is mostly for some new comers.
The basic idea of this setup is selecting the categories from one table and list them. Then you insert each check box (category) into one field, separated with a comma. Then you explode the categories when you want to display it. You need to first setup your tables. Make one table called Categories, with 2 fields. Primary ID, and Name. (You can do a 3rd - description, if you want). Then if you already have a posts table for your CMS, just make a new field called "cats" or "categories". On your page where you add a new post, add the following code. PHP Code:
(Note: This needs to be inside the form you use to post your articles). Now before you insert this data into the database, we need to link all the check boxes together. Heres how we can do it. PHP Code:
Now its time to actually call the categories. Here is how we can do that: PHP Code:
First we define the variable $cats as an explosion of the field "cats". Remember when we imploded it before we put it into the database, now we are doing the opposite. We are finding the "," between each one, and splitting it up into an array. Then we count it with $totalCats, so we know how many pieces it was exploded into. The $i variable is just set to 0, for counting purposes. The next piece of code is called a while loop. It means this. PHP Code:
PHP Code:
The very last $i = $i + 1; is to increment our $i variable, so the while loop will move onto the next exploded piece. That about does it. :) I know the tutorial may be sort of hard to follow, since I assumed you had a few things setup already, but I didn't have time to write out a whole CMS tutorial, since I am at school. :-P But if you have any questions on how to get it working, I'll be glad to answer them as best I can. Good luck! ^^ |
Good job on the tutorial.
Thanks. :-) |
Thanks for the tutorial! There is another way to get around this problem, and that is by normalising your database. That is, having a lookup table to store all the items. You list all your categories in one table (categories) and then you list all your items that require categories in another table (pictures), you then have a table in between those 2 called something like (pictures_categories) - your lookup table.
Allow me to demonstrate. Code:
Table: categoriesCode:
Table: picturesCode:
Table: pictures_categoriesJOIN to get the categories that the images are associated with. Something like so:Note: Not tested this query, but I'm 95% sure it'll be fine! sql Code:
This would then return the names of all the categories that image1 has associated with it. It's a tough one to describe, but I hope you can at least extract some useful points! Also for any one interested, maybe Wikipedia'll provide more in-depth information. But basically, our pictures_categories table contains 2 columns of IDs which reference each of the other 2 tables. You just match up the IDs and use the lookup table to store multiple categories. |
Ah good idea Wildhoney. :) I guess in a way that would be a little more organized then the method I mentioned. 8-)
@ Haris - No problem! ^^ |
All part of the learning process, squire! :-) If you'd not have written the article you wouldn't have found the other way of going about it. Not today anyway. The good thing about my way is it's really fast to find the categories and even search within the categories.
Article much appreciated! |
Quote:
In my last script, I had like 3 queries to do simple tasks like these. :-! |
So Wildhoney, on yours, each time you add something to the pictures database, you get just the id, and then put the id in the pictures_category table, with the id of category? Then when you want to query them.. How exactly are you matching them? Maybe I need to read up on table joining. :)
EDIT: Ahh yes.. I've just read a few short articles. Join and Left Join looks pretty interesting, I may have to start putting it to use. :) |
Sorry, completely missed your last question, Gurnk! Basically it's all down to the
JOINs:
Key: Blue = Table Green = Column So for every ID that matches to the picture's image in our lookup table, get all the IDs that match up to a categories in the categories table. |
Hum, sorry to be a burden.. but you should probably test queries before you suspect that they work, it could save time in the future ^^
[/TIP] Otherwise, great article for the beginners! |
Quote:
|
Quote:
|
Quote:
|
How do you mean, Haris? That structure can be used for anything although usually it's where you have a one-to-many relationship, you introduce the lookup table to normalise the database to allow for two one-to-one many relationships across 3 tables.
|
Code:
category |
I've got it now.
|
| All times are GMT. The time now is 10:00 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0