How to build your own tag cloud

I was developing a site for one of our client. The client need a tag cloud for the site. Then i started searching for a tag cloud in the net, but can’t find a suitable one for me. So i decided why not build my own tag cloud. Then i started coding a tag cloud for my own.
The main process is done by the code below:

/*
*Author: A.H.M. Rakibul Islam
*Email: rubelonline@yahoo.com
*Build Date: 23-07-2008
*/

$thistag = $_GET['tag']; //Getting the tag

$result = mysql_query(“select * from tags where tag = ‘$thistag’”);

while($row = mysql_fetch_assoc($result))
{
if($row['tag'] == $thistag) //if $thistag matches with existing tag
{
if($row['hit'] < 30) // and is less the 30 hit then update the hit = hit +1, because this hit is eventually be the font size;
{
$updated_hit = $row['hit'] + 1;
mysql_query(“UPDATE `tags` SET `hit` = ‘$updated_hit’ WHERE `tag` =’$thistag’”);

$updated = 1;
}//end of if($row['hit'] < 30)
}//end of if($row['tag'] == $thistag)
}//end of while($row = mysql_fetch_assoc($result))

if(!isset($updated)) //If $updated != 1, that means it is a new tag and it will then need to inserted to the table with it’s respective link
{
$arr = explode(“#”,$_SERVER['REQUEST_URI']); //capturing the current address to create link for the tag
$link = “http://localhost$arr[0]“; //it will create http://localhost/sitename, which creates respective link for the tags
//$link = “http://mysite.com$arr[0]“; //for real webserver
$hit = 10; //Initializing the hit from 10 because it will be also the initial font size of the cloud

mysql_query(“INSERT INTO `tags` ( `id` , `tag` , `link` , `hit` )
VALUES ( ”, ‘$thistag’, ‘$link’, ‘$hit’ );”);
}

And for showing the cloud i wrote the following code:


session_start();
include(‘include/connection.php’);

$result = mysql_query(“select * from tags”);

while($row = mysql_fetch_assoc($result))
{
//style=”font-size:’.$row['hit'].’px” is creating the font size of the tag.
echo ‘‘.$row['tag'].’ ‘;
}

You can download the tagcloud by clicking here

No comments yet

Leave a reply