• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Building Web 2.0 Tag Clouds in PHP

By Akash Mehta | on May 2, 2008 | 9 Comments
PHP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

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:

php
javascript

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:

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.


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.


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.


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:


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:

 $value) {
        $size = round($minsize + (($value - $minval) * $step));
        echo ''.$key.' ';
    }
}

$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.

Share this story:
  • tweet

Author Description

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

  1. May 2, 2008

    Jesse Mullan Log in to Reply

    What happens when $maxval == $minval ?

  2. May 5, 2008

    Akash Mehta Log in to Reply

    @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. May 5, 2008

    Jesse Mullan Log in to Reply

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

  4. August 19, 2008

    Ridvan ROGER Log in to Reply

    It is really a simple solution, but a good soulution that is exactly; extract all words from a page as values and listing it.

  5. May 30, 2009

    Adam Log in to Reply

    Thank you for sharing Akash, I appreciate it.

  6. June 6, 2009

    AMANdeep MAnn Log in to Reply

    It is really a simple solution, but a good soulution that is exactly; extract all words from a page as values and listing it.

  7. August 20, 2009

    mike Log in to Reply

    ok, but how do you get the links working in this tag cloud? now you have # but we needs links. Lnks would be smth like “link.php?id=tag_id” – you are only passing two arguments to function, links or tag id are missing

  8. December 23, 2009

    NZGeek Log in to Reply

    mike, you would have to build an array of links, e.g.
    $t = array(“php” => “http://link”, “orm” => “http://link”); etc

  9. February 6, 2010

    Joel Log in to Reply

    @mike – what do you want to link to? I’d suggest putting together a “tag page” that lists entries by associated tags. So if you click on PHP it brings you to all posts with PHP tagged.

    It’s easy to link, just put “tagpage.php?v=” . $key . “” and then on your tagpage.php just have one query that uses the variable $key to define the query parameters.

    Hope that helps!

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 1,240Followers 494Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.