TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 12-03-2007, 01:10 PM   #1 (permalink)
The Contributor
Upcoming Programmer 
 
Gurnk's Avatar
 
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
Gurnk is on a distinguished road
Book 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:
<?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!
Send a message via MSN to Gurnk
Gurnk is offline  
Reply With Quote
The Following 3 Users Say Thank You to Gurnk For This Useful Post:
codefreek (12-29-2007), Haris (12-03-2007), Wildhoney (12-03-2007)
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:31 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design