A popular extra for articles and posts to have on a website is tags, one- or two-word descriptive items that associate the post content with a broader category, such as an article about Obama being tagged as president, white house, government, etc. This article will walk you through a simple design for implementing tags for your posts.

Assumptions

  • You have a knowledge of PHP and MySQL
  • Access to the site's database
  • A table for posts already exists in the database
  • Each post has a unique ID as part of the table structure
  • There is a post-submission form on the site

What we want to do first is create a new table called tags. You will give this table two fields:

  • post_id - the ID of the post (SMALLINT *change if desired)
  • tag - the tag name (VARCHAR 40 *change if desired)

You can leave all other settings as is and save the table structure.

Now to create the code for adding tags to the database when you submit a post.

First, we need to add a field to the post-submission form for submitting tags.

<input type='text' name='post_tags' />

You may wish to increase the length of this box by including a size=# in the tag. (Make sure you modify your POST code accordingly to submit the post_tags value along).

Time to create a couple PHP functions. The first will be used to create an array of tags based on the submitted tags

<?php
// $post_tags is the value from post_tags in the submission form
function get_tags_array($post_tags)
{
  
// explode splits a string at the specified character
  
return explode(", "$post_tags);
}
?>

In the code above, the string of tags is submitted and returned back to us as an array with one tag per array value. For this example, the tags are split at every instance of a comma followed by a space. This means that if our tags looked like this in the submission form

president, government, white house

get_tags_array() would return this array

Array
(
 [0] => president
 [1] => government
 [2] => white house
)

We need this function in order to split up the string of tags into separate tags so they can be inserted into the tags table we created earlier.

The next function is the one that will add the tags to the table

<?php
// $post_id is the ID of the post being submitted
// $post_tags is the post_tags string
function submit_tags($post_id$post_tags)
{
  
// our function from earlier
  
$tags_array get_tags_array($post_tags);

  
// add a row to the tags table for each value in the array
  // count() returns the number of values in the array
  
for($i 0$i count($tags_array); $i++)
  {
    
$query "INSERT INTO `post_tags` (post_id, tag) VALUES('$post_id', '".$tags_array[$i]."')";
    
$result mysql_query($query) or die(mysql_error());
  }
}
?>

The above function first gets the tag array, then adds a new row for every value in the array.

Staying consistent with our presidential tags, after we run submit_tags() above, our tags table will look like this (let's assume that the Obama post has a post_id of 1)

post_id     tag
---------   ------------
1           president
1           government
1           white house

That's it! Now you have a tagging system for your site! Below are a couple queries you can use for retrieving data from this table

<?php
// get all tags for a specific post
$post_id = <insert your post_id here>
$query "SELECT * FROM `tags` WHERE `post_id` = '$post_id";

// get all posts with a specific tag
// the table 'posts' is where all other information is held (i.e. the post title and content
$tag = <insert tag name here>
$query "SELECT DISTINCT(posts.post_id), posts.* FROM posts RIGHT JOIN tags ON tags.post_id = posts.post_id WHERE tags.tag = '$tag'";
?>
Enjoy!