Skip to content

TagClouds in PHP Revisited

Dear Reader,

Yes I know TagClouds are so 2005; and yes I know TagClouds are the new Mullet; they are still fun to play with. With that having been said, I went back and re-factored the PHP code I wrote a while back for my AJAX resume. For that project, my sole purpose was just to get a TagCloud of my skills and any old TagCloud would do. This time, my purpose was to build a class to automate it as well as build an example to show how useful/useless it really is. I’ve accomplished both of those lofty goals. Here is the source code and here is the example.

Originally I was going to spend a lot of time explaining to you how I did it in an insipid tutorial. I realized though that you dear reader, fall into one of 2 classes of people.

If you are a programmer then the last thing you want is for me to explain it to you when you can read the source yourself. Therefore, if you are in this camp, just download it and read it. I’ve tried to comment it thoroughly but if you have any questions, don’t hesitate to drop me a line.

If you are in this group then you don’t really care how it works, you just want to know how to use it. First, check out the example page. The keywords on the sample page come from the last 30 entries of my blog. I’ll admit I cheated and just open the database and pull them from there instead of trying to call my blog w/curl or fopen and parse it. (Although that would have been cooler.) I then ran each blog entry against Yahoo’s Term Extraction API to get the terms. Then I pumped that array into my new TagCloud maker. The important code you can’t see from the index.php is below.

$x = TagCloud::fetch_cloud($thisArray,'...',
$o = new TagCloud($thisArray,'...','tagcloud_',
$y = $o->output_cloud;

As you can see, I used 2 different methods of creating the 2 lists. The first is just calling the object::method statically with all the necessary parameters. This is by far the easiest way to use this class and unless you need something special, it's the way I recommend. The second way it to instantiate an object. I pass all the variables into the constructor but you could just as easily use the setters to set them all. (don't know why you would but hey, I spent an extra 15 minutes creating getters and setters so use them, by all means use them!)

Then just use the contents of $y somewhere in your page. (To those of you who have worked on teams I have built, yes, I know I always preached that it's a fire-able offense to use a single letter variable name but since I don't work for you I don't care!) :)

That's it. Use it or don't. I don't care; I had fun writing it. As is my custom, I think I'll turn it into a WordPress plugin. Because god knows we need tag clouds in our blogs!

Until next time, I love you even when you piss me off.

p.s. This blog was originally titled "It's Raining Tags" which was a lot more fun but not nearly as descriptive.

p.s.s. Proof that great minds think alike I found this. The site is in (Japanese?) but the code is in...php.