spacer
Web Development Blog
 Developer Newsletter

Webmaster Blogs
Content & Blogging
Design
Photoshop
General
JavaScript
PHP
PHP Functions
Web
WordPress
Website Promotion

Blog Archives


Developer Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous

Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Webmaster Blog

Building Web 2.0 Tag Clouds in PHP

Every major website seems to have a tag cloud. Users love tag clouds; they help navigate masses of content quickly and easily. When used appropriately, they help us sort filter through information stores, reduce the signal-to-noise ratio. Providing a tag cloud is also relatively painless for the server - after all, it’s just some HTML. But how do we actually build a tag cloud at application level? In this tutorial, I’ll take you through putting together a full-blown, calculated web 2.0 tag cloud in PHP.

First, let’s look at what we want to end up with. We’ll take this data:

PHP: 30 items
JavaScript: 24 items
Java: 17 items
Python: 26 items
Ruby: 17 items

And we want to end up with this tag cloud:

php javascript java python ruby

To do this, we need to first examine what a tag cloud is like at HTML level. Each of these tags needs to be wrapped in an element that can have a style attribute. We’ll use that to set a font size. We could use either percentages or pixels; both have advantages and disadvantages. In this tutorial, we’ll do something like this:

<a href="#" style="font-size: 40px">php</a>
<a href="#" style="font-size: 30px">javascript</a>

We’re using anchor tags so that we can later link them as needed.

So let’s start with our data. The above tag cloud was generated with this array:

<?php
$tags = array('php'=>30, 'javascript'=>24, 'java'=>17, 'python'=>26, 'ruby'=>17);

The next step in a tag cloud is calculating those font sizes. We need to define a minimum and maximum value for that font-size CSS parameter, so that we always know what sizes we’re dealing with. We then calculate the minimum and maximum values from the entire data set - array_values() will help us sort out the data - and the difference between these, the spread of the data.

<?php
$maxsize = 30;
$minsize = 20;
 
$maxval = max(array_values($tags));
$minval = min(array_values($tags));
 
$spread = $maxval - $minval;

Finally, we work out a “step”, or an increment value for each level of items. This step will be calculated based on the difference between minimum and maximum font size, divided by the spread. This gives us a relationship between number of items and pixels for font size.

<?php
$step = ($maxsize - $minsize) / $spread;

We can then work out the font size as the difference between the number of items for the current item and the lowest number of items (the minimum) in the set, multiplied by the step. This gives us the value of the font size of the current item in relation to 0. Finally, we add the minimum font size value for some width.

<?php
round($minsize + (($value - $minval) * $step));

Let’s take an example for a second. Take a new dataset, with javascript 5, php 6 and java 7. Our minimum font size is 20 and maximum size 30. Therefore $minval is 5, and $maxval is 7. Our $spread is 2, or 7-5. Our $step is ($maxsize - $minsize) / $spread, giving us 10 - 2 or 5. This means there is 4 pixels difference between each of our three values. PHP’s value would be calculated like this:

<?php
round($minsize + (($value - $minval) * $step));
round(20       + ((6      - 5)       * 5));

This gives us 25, or 25 pixels. It’s quite simple, really. Let’s put all this code together, including a simple loop to run over each of the tags and spit out the HTML within a function:

<?php
function tag_cloud($tags) {
    $maxsize = 40;
    $minsize = 20;
 
    $maxval = max(array_values($tags));
    $minval = min(array_values($tags));
 
    $spread = $maxval - $minval;
 
    $step = ($maxsize - $minsize) / ($spread);
 
    foreach ($tags as $key => $value) {
        $size = round($minsize + (($value - $minval) * $step));
        echo '<a href="#" style="font-size: '.$size.'px">'.$key.'</a> ';
    }
}
 
$tags = array('php'=>30, 'javascript'=>24, 'java'=>17, 'python'=>26, 'ruby'=>17);
 
tag_cloud($tags);

And we’re done! A simple yet powerful PHP script to generate a web 2.0 tag cloud for your website.


Related Posts
» Kickstart Your Link Building Campaign
» Why you should be using YAML for config
» Open Source Social Networking Server: Built on LAMP!
» What makes a perfect backlink
» Creating a Realistic Sun in Photoshop
 


3 Responses to “Building Web 2.0 Tag Clouds in PHP”

  1. Jesse Mullan Says:

    What happens when $maxval == $minval ?

  2. Akash Mehta Says:

    @Jesse: that’s an interesting point; when $maxval == $minval, we’d have a division by zero when calculating $spread.

    However, the only time this could happen is a) when you only have one tag; and b) when all your tags have equal values.

    For the first scenario, I leave the validation to the developer as you would probably want to do something when you have only one tag (e.g. list items within the tag).

    For the second scenario, either your site is new, you have hit quite a coincidence or you don’t need a tag cloud in the first place (because all your “tags” have equal values). Once again, I leave this up to the developer to handle outside of the function, as the function’s sole purpose is to generate a tag cloud, and I don’t wish to complicate it by adding error handling (or worse still, making it do nothing - blank outputs are the hardest to debug).

    Of course, if this is an issue and you choose to handle it within the function, all it takes is an if($spread == 0) $spread = 1;.

  3. Jesse Mullan Says:

    I knew the answer already — I was wondering if you had considered the edge cases.

Leave a Reply





About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: