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:
<?php
$select = @mysql_query("SELECT title FROM `cats` ORDER by title DESC");
while($row = mysql_fetch_array($select)) {
extract($row);
echo'<input type="checkbox" name="cat[]" value="'.$title.'" />'.$title.' <br />';
}
?>
This code will get all the rows from the "cats" table, and list them next to a check box. The name of the check box is an array, so you can have multiple checks.
(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:
// Make an array out of our name="cat[]" value on the checkbox.
$cats = array_map("mysql_real_escape_string",$cat);
// Implode (Opposite of explode) them together with a comma.
$imp = implode(",",$cats);
Now just insert the "$imp" variable into the database field that you made (probably cat) like normal. So if you checked "Site Updates" and "Random Info", it would insert "Site Updates, Random Info" into the single field.
Now its time to actually call the categories. Here is how we can do that:
PHP Code:
$cats = explode(",", $cats);
$totalcats = count($cats);
$i = 0;
while($i < $totalcats) {
echo'<a href="browse.php?view='.$cats[''.$i.''].'" >'.$cats[''.$i.''].'</a>';
if($i == $totalcats - 1)
echo "";
else
echo ", ";
$i = $i + 1;
}
All of that above code should be inside your loop that is getting your posts.
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:
while(something is true) {
do this
}
So, while $i (0) is less than $totalCats, continue. The first echo in our loop echos out a link and the name of the category. $cats[''.$i.''] is getting whatever value is represented in our array by cat[0]. In arrays, the counting starts as
0 So that means it is echoing our first piece of exploded field. The next part:
PHP Code:
if($i == $totalcats - 1)
echo "";
else
echo ", ";
This is a little cosmetics thing. Its saying that if $i (our number) is 1 less than the total number of exploded pieces, echo nothing. If it is not, echo a ", ". It needs to be one less, since arrays start counting at 0. This means if you have 3 categories, "Cat1", "Cat2", and "Cat3", it will be "Cat1, Cat2, Cat3" It didn't add that last comma, since it realized Cat3 was the last one.
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.

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!
