Scriptplayground Network

Scriptplayground Preview
Fireworks CS4 Learning Center

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area. This book explains the process of working with PHP in Flash, while creating real world examples that you can actually learn something from.

The Flash and PHP Bible has a dedicated forum for support and comments.

Scriptplayground » tutorials » php » Tag Cloud

Tag Cloud

Build one of those popular tag clouds as found on sites such as: "Flickr", "del.icio.us" and others.

View an Example of this article before you get started.

A tag cloud is a list of tags that are sized based on keyword count. For this example we will be using a static array of tags, but this can easily be extended to a database application.

We will start off with the CSS which will provide the visual element to this tutorial.

<style type="text/css">
.tag_cloud { padding: 3px; text-decoration: none; }
.tag_cloud:link  { color: #81d601; }
.tag_cloud:visited { color: #019c05; }
.tag_cloud:hover { color: #ffffff; background: #69da03; }
.tag_cloud:active { color: #ffffff; background: #ACFC65; }
</style>

Next up, a function that will return an Array we will be using. As you will see it is a simple Multi-dimensional Array that stores the tag/count pairs.

function get_tag_data() {
    $arr = Array('Actionscript' => 35, 'Adobe' => 22, 'Array' => 44, 'Background' => 43, 
	'Blur' => 18, 'Canvas' => 33, 'Class' => 15, 'Color Palette' => 11, 'Crop' => 42, 
	'Delimiter' => 13, 'Depth' => 34, 'Design' => 8, 'Encode' => 12, 'Encryption' => 30, 
	'Extract' => 28, 'Filters' => 42, 'Flash' => 32, 'Functions' => 19, 
	'Gaussian Blur' => 44, 'Grafix' => 49, 'Graphics' => 35, 'Hue' => 47, 'Illustrator' => 8, 
	'Image Ready' => 12, 'Javascript' => 47, 'Jpeg' => 15, 	'Keyboard' => 18, 'Level' => 28, 
	'Liquify' => 30, 'Listener' => 10, 'Logo' => 12, 'Loops' => 22, 'Macromedia' => 26, 
	'Method' => 28, 'MySQL' => 18, 'Obfuscation' => 13, 'Object' => 39, 'Optimize' => 25, 
	'PDF' => 37, 'PHP' => 44, 'PSD' => 17, 'Photography' => 28, 'Photoshop' => 46, 
	'Revert' => 50, 'Saturation' => 35, 'Save as' => 28, 'Scope' => 11, 'Scripting' => 9, 
	'Security' => 41, 'Sharpen' => 49, 'Switch' => 41, 'Templates' => 11, 'Texture' => 22, 
	'Tool Palette' => 30, 'Variables' => 50);
    return $arr;
}

Now that we have the array function all setup, we move on to the function that will return our cloud tag HTML

function get_tag_cloud() { ... }

Lets run through this big function and explain what is going on. First off we define the min and max font sizes.

// Default font sizes
$min_font_size = 12;
$max_font_size = 30;

Call our "get_tag_data" function to retrive an array of tags.

// Pull in tag data
$tags = get_tag_data();

We pull out the minimum and maximum count from our tags. Creating a spread to be used in our font size calculations. Check to see if we have a good spread, if not, set one.

$minimum_count = min(array_values($tags));
$maximum_count = max(array_values($tags));
$spread = $maximum_count - $minimum_count;

if($spread == 0) {
    $spread = 1;
}

Finally we start the HTML building process to display our tags. For this demo the tag simply searches Google using the provided tag. We then return the HTML code.

$cloud_html = '';
$cloud_tags = array(); // create an array to hold tag code
foreach ($tags as $tag => $count) {
	$size = $min_font_size + ($count - $minimum_count) 
		* ($max_font_size - $min_font_size) / $spread;
	$cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' 
		. '" class="tag_cloud" href="http://www.google.com/search?q=' . $tag 
		. '" title="\'' . $tag  . '\' returned a count of ' . $count . '">' 
		. htmlspecialchars(stripslashes($tag)) . '</a>';
}
$cloud_html = join("\n", $cloud_tags) . "\n";
return $cloud_html;

Here is some example HTML to call this tag builder.

<h3>Sample Tag Cloud results</h3>
<div id="wrapper"
<!-- BEGIN Tag Cloud -->
<?php print get_tag_cloud(); ?>
<!-- END Tag Cloud -->
</div>

There you have it a step by step guide on how to write your very own Tag Cloud... until next time... Happy Coding.

Comments: Tag Cloud

 thomas  Fri Jul 7, 2006 2:39 pm  
okay, well, i really like this and you did a great job. but i want to make it work with my database. so i've added a column to my "pages" table and i now have a feild to input the tags 4 that page when u make it, but how do i get the number of occurances to average out from the database. i'm a little confused. actually, do u think u can quickly add how to use it with a database. thanks in advance.
 thomas  Fri Jul 7, 2006 10:35 pm  
please somebody help. If you do i can probably promote your site or do a custom web deign or smoething for you, please, i just really want to do this. thank you......
 Matthew Keefe  Sat Jul 8, 2006 1:01 am  
Hey Thomas,

Thanks, that is a great idea.

I added a new tutorial extending this one to use a MySQL database instead of the static array.

Tag Cloud with Database

Matt
 thomas  Sat Jul 8, 2006 1:58 am  
well, i actually wrote it my self based on a bunch of other tutorials and then put them all together, but i really appreciate you writing the new one jsut for me, thank you so much. you can email me at thrasher.basher@gmail.com if you'd like to talk about advertising or whatever. and i'm on aim at iplayguitar214 if u ever just want to chat. Thank you so much matt. Ur a lifesaver.
And i don't think your comment capacha uses random strings because it's been the same everytime.
 Matthew Keefe  Sat Jul 8, 2006 7:19 am  
Glad to hear you got it worked out and thanks again for the idea. That is how this site works, the reader suggests it and we write it.

In regards to the anti-spam measures, I am aware of that and also know the cause but am going to let it stay until the new site is launched. Thanks for the heads up though.

Matt
 cem  Thu Nov 23, 2006 10:59 am  
Does this skript count the klicks on the tags and changes them in the database? Please help me...
 Matthew  Thu Nov 23, 2006 1:35 pm  
No, it is used to display the popularity of a tag. However if you wanted to use it to track a tag then you would set up a function to increment (update) the database entry whenever it is clicked.
 Bill  Mon Dec 11, 2006 3:50 pm  
Newbie here so please excuse me... is there an example PHP file of this code to view so that I can see it "constructed" correctly?
 Matthew  Mon Dec 11, 2006 4:27 pm  
Not a problem, here is the example PHP file. TagCloud-source
 Bill  Thu Dec 14, 2006 5:48 am  
Cheers, Thanks very much Matthew ;)
 Akash Takyar  Tue Feb 20, 2007 4:28 pm  
Thats a perfect job. Thank you Matthew.

 mkeefe  Sun Feb 25, 2007 12:19 am  
Not a problem, this has been a popular one.
 FP Images  Thu Mar 22, 2007 12:42 pm  
You can employ more than one color with discretion.
One way to use colors in your cloud tag:
- select a main color, assign it to the biggest font;
- "blend" this color for smaller fonts.
A "coloring" example at
Tag cloud. Key words visualization
 anita  Sat Apr 14, 2007 11:22 am  
can anyone please help me.. how to make this work...i mean i have to paste all code in html files or have to make sepreate css, js php n html file

please help me out
Thanks
 mkeefe  Sat Apr 14, 2007 1:11 pm  
I am on the road at the moment so putting an extended usage together would take some time, however I have posted 1 file that has everything you need to recreate the static array version (seen on this page).

http://www.scriptplayground.com/article_files/Tag-Cloud/TagCloud-source.phps

Hope this helps.
 anita   Sat Apr 14, 2007 6:22 pm  
Thanks alot now i m tryiing to make it database version .. lets see if i can paste if correctly..
will need again help if anything goes wrong..
 Gerald  Fri Apr 27, 2007 9:29 am  
Hi,

Forgive my apparent retardation but I get an error in line 8 when I copy and paste the original.

It's the

function get_tag_data() {

line
 mkeefe  Fri Apr 27, 2007 9:55 am  
What error do you get?
 Gerald  Fri Apr 27, 2007 9:56 am  
Parse error: syntax error, unexpected T_STRING in blah/blah/blah/test/index.php on line 8
 mkeefe  Fri Apr 27, 2007 11:49 am  
Hmm, make sure you have an opening and closing ' on all of your tag words. Also make sure you have a closing ] at the end of the array.

Hope that helps and fixes it.

Let me know
 Gerald  Fri Apr 27, 2007 12:56 pm  
Hi,

Actually, it's really copied and pasted correctly. No ] anywhere around array section, though.

It seems to work for others - so no worries. I'll check it out when there's more time. Thanks.

Do not know if this is possible but could you php a tutorial for a script that would search a webpage for the category name or a css tag and then produce the cloud from that?

Thanks,

Gerald
 Marco  Fri Jun 22, 2007 2:49 pm  
Is there any solution in asp ?

thanks
 Mr Bill  Wed Jul 11, 2007 4:31 pm  
Thank you will try to sort this. Would be nice to have a straight forward download if that wold be possible
 Mellowman  Wed Aug 22, 2007 5:45 pm  
Great tutorial but I was confused on how to put all this together. Could not get it to work.
 RigRank  Sat Oct 6, 2007 6:27 pm  
Thanks. Anyone know how to make the URL connect to some kind of search page as seen in del.icio.us ?
 roberto  Thu Nov 1, 2007 3:42 am  
I tend to get better looking tagclouds by using log(count) instead of just count as described in

http://blogs.dekoh.com/dev/2007/10/29/choosing-a-good-font-size-variation-algorithm-for-your-tag-cloud/
 Monkey Tuk  Sat Nov 10, 2007 9:33 pm  
Good work man. It's running 100% thanks.
 Edward Han  Tue Nov 27, 2007 3:17 am  
I have Tag field where users can input their Tags separated by comma. Example:
hat, shirt, clothes, dress

This will be saved in the Tag field. I have used your example above and when it shows, instead of showing one word per Tag, it is showing as the entire words per Tag.

How can I separate them to be individual word per Tag?

Thanks!
 mohamed  Fri Nov 30, 2007 12:17 am  
welcome
thanks for this nice tutorials i joined my tags with search engine in my script
i have small problem in title page with arabic cha when you open it with ie 7 (like google cach link&title)
my title page show like
ظ…ظƒظٹط§ط¬-العاب
this is command which show my title in the search page
$sitename2 = $searchTerm .
if you can help me tell me how to display this in encoding UTF-8
$sitename2 = $searchTerm .
thanks
 mohamed  Fri Nov 30, 2007 12:19 am  
sorry the example for title didnt show it look like
ظ…ظƒظٹط§ط¬
 mkeefe  Fri Nov 30, 2007 8:45 am  
This should help you:

http://www.hotpeachpages.net/a/characters.html

Matt
 mohamed  Fri Nov 30, 2007 9:12 am  
i think you not understand me
i want to slove it in the script
can you look here please and show how my title appear
http://www.google.com.eg/search?hl=ar&rlz=1T4GGLJ_enEG247EG247&q=site%3Agame.paramegsoft.com%2Ftags%2F%D8%A7%D9%84%D8%AC%D8%A7%D8%B3%D9%88%D8%B3%D8%A7%D8%AA&meta=

 mohamed  Fri Nov 30, 2007 12:12 pm  
sloved
i was want something like
$str = mb_convert_encoding($str,"encoding1","encoding2");
it ok now
thanks Matt
 mohamed  Fri Nov 30, 2007 1:11 pm  
if i want to add (-) between word and another how i do it
example
if this word the tag PHP TUTORIALS

i want it show in the link like

http://www.scriptplayground.com/tags/PHP-TUTORIALS

how i can do that
thanks
-sorry for my many questions and comments
 cedric x  Mon Dec 17, 2007 9:46 pm  
my problem really solve this tutorials, but one more thing, i want to change the density of the word for example i search the word "computer", all word that are related to computer will be bolder or bigger than the other. Will that be possible?
 Pat  Wed Jan 16, 2008 7:46 am  
Here is a nice tag cloud script with mysql database, easy to configure :

http://www.ilikeyoutube.com/cloud/

Enjoy.
 sam  Sun Feb 17, 2008 1:48 pm  
Great idea.
 Jeff  Sun Feb 24, 2008 6:55 pm  
I like this Tag cloud , Thanks for posting it .

Regards,,
 flash games  Thu Mar 20, 2008 5:29 am  
Thank you for Nice Post
 sharly  Sun Mar 23, 2008 12:06 pm  
god
 Myspace Proxy  Sun May 18, 2008 3:10 am  
So why would you advertise your script here on his site? I like this script is this project still being maintained or is ther further development in the future?
 chuck  Sun Jun 1, 2008 1:39 pm  
Your script is great and works fine from a mysql table but when I try to display it in a table cell the tags overrun the width of the cell. I've tried defining the width of the cell from inside the cell tag to in css. In your posted example, what html container for the cloud is being used. Thanks for the post.
 mkeefe  Sun Jun 1, 2008 3:28 pm  
@Myspace Proxy, yes, this script is still maintained.

@chuck, Display in a table cell? Do you mean an HTML table? The example is a div with a forced width.
 sunjester  Sun Jun 15, 2008 12:46 pm  
not your best, but its not that bad. im kinda curious as to why you stoired them all in an array like that as opposed to using a database or flatfile, xml would have been perfect for this.
 mkeefe  Sun Jun 15, 2008 12:48 pm  
@sunjester, I used an array approach to explain the tag cloud concept. I try to limit how much work I do for the reader, otherwise they never learn.

However, I created a database example after a bunch of comments came in requesting it.
 Tutorials Room  Mon Jun 16, 2008 2:29 pm  
Nice tutorial! It was chosen for the main page of http://www.tutorialsroom.com and under Web Development tutorials. Please feel free to submit more of your good tutorials using this link: http://www.tutorialsroom.com/tutorials/submit_tutorials.html.
 Horoscope  Thu Jun 26, 2008 9:19 am  
These are very interesting information. Thank you for having published.
 gescom  Thu Jul 17, 2008 4:59 pm  
Great script!!
Would it be possible to use "txt" or "rss" source instead of MySql?

thanks,
cheers
 ilahiler  Mon Jul 21, 2008 3:38 pm  
thankssa
Add a comment
Name:
Website:
Comment:
Please note: Offensive comments, flaming and spamming is not permitted on this site and your comment will be deleted immediately.

HTML is not allowed.

Please provide all comments in English so that others can help you. A common helper in this is to use an online translator.

As a security measure your ip will be recorded.
 
Anti-Robot Check:

Enter the key you see above.

What is this?: This extra test has been added due to the recent explosion of spam.
 
Google