The UHF of the film world.


quietearth [General News 03.30.07]

Share on Google+


This is a simple guide on how to set up tags for your posts, but it goes beyond just that. It will work with any table with integer id's, and multi-user systems. Freetag is pretty flexible, and has calls to make tag clouds (with autosize based on tag count) as well as related tags. It's very useful, and easy to setup.

First off let's grab the latest source code for freetag from google:
http://code.google.com/p/freetag/


Then gunzip/untar the package, and I'm assuming you have your database already setup, so let's import the freetag table. Change into the freetag root dir:
# mysql -p databasename < freetag.sql

This creates two tables, freetags, and freetagged_objects.

The freetags table are the actual tags we create. The tags are in "normalized form" and "raw" form". If we created a tag such as "post apocalyptic", this would be the raw tag, and it would add a normalized form "postapocalyptic".

The freetags_objects table is where items are actually tagged. Now we have the option here of having a specific "user id" tagging objects, so we can break things down by user, or you can just use the default so it doesn't split into different taggers. The latter is what you would use for your blog, since your the only one doing the tagging, but if you wanted to build a site where users could create their own tags, freetag can do it.

Now I create a mytags.php file to include in other php for easy access to the freetag objects:
require_once("/path/to/freetag-version/freetag.class.php");

function freetagnew() {
        $freetag_options = array (
                'debug' => FALSE,
                'db_user' => 'testuser',
                'db_pass' => 'testpassword',
                'db_host' => 'localhost',
                'db_name' => 'testdb');

        $freetag = new freetag($freetag_options);
        return($freetag);
        }


Now from here it's simple, to add tags:
include("mytags.php");

$ft = freetagnew();
$ft->tag_object(1, $id, $tags);


1 is the user id, we will use the same id on all objects since we aren't splitting tags up between different users. $id is the id of the object from whatever table your tagging. $tags is a raw string of tags, we could even get this from a $POST, it can be in this form:
"post apocalyptic" movie review
and it will put 3 tags on the object.

To delete all the tags on an object:
$ft->delete_all_object_tags($id);

To get the tags on an object:
$tags = $ft->get_tags_on_object($id, 0, 0);

if (count($tags) > 0) {
     foreach ($tags as $tag) {
          print $tag['raw_tag'] . "\n";
          print $tag['tag'] . "\n";
          }
     }


To get the object id's with the same tag:
$tagarray = $ft->get_objects_with_tag($tag);
$num = count($tagarray);
for ($i = 0; $i < $num; $i++) {
     print "tag id ".$tagarray[$i]."\n";
     }


And last but not least, lets make a tag cloud:
$taghtml = $ft->get_tag_cloud_html(40, 9, 20, "px", "cloudtag", "/tag/");

The first param is the number of tags to return.
The second param is the min font size.
The third param is the max font size.
The fourth param is the units (eg px or em).
The fifth param is tag page url to prepend.
Now just print $taghtml, and you've got your cloud.

If there are any questions, please feel free to post them.


Leave a comment








Related articles