Community Documentation

Tag cloud with a count of the number of times a tag has been used

Last updated October 14, 2011. Created by oskar_calvo on November 15, 2007.
Edited by MGParisi, LeeHunter, bekasu, danschaller. Log in to edit this page.

Drupal 7.X module http://drupal.org/project/tagclouds provides this functionality

Drupal 5.X
This is a PHP snippet to get a tag cloud that provides a count of the number of times a tag has been used.

<?php
class tag
{
    var
$name = "";
    var
$count = 0;
    var
$tid = 0;

    function
getCount()
    {
        return
$this->count;
    }
    function
getName()
    {
        return
$this->name;
    }
    function
setCount($var)
    {
       
$this->count = $var;
    }
    function
setName($var)
    {
       
$this->name = $var;
    }
    function
setTID($var)
    {
       
$this->tid = $var;
    }
    function
getTID()
    {
        return
$this->tid;
    }

}
$count = 0; // Overall count of used tags
$threshold = 1; // How many tags are needed to get displayed
$font_size = 0.8;
$query = "SELECT d.name,d.tid FROM {term_data} d, {term_node} dn WHERE dn.tid=d.tid;";
$result = db_query($query);
$tags['Test'] = 0;
while(
$node = db_fetch_array($result))
{
    if (
$tags[$node['name']] == NULL)
    {
       
$tags[$node['name']] = new tag();
       
$tags[$node['name']]->setName($node['name']);
       
$tags[$node['name']]->setCount(1);
       
$tags[$node['name']]->setTID($node['tid']);
       
$count = $count + 1;
    }
    else
    {
       
$tags[$node['name']]->setCount(
           
$tags[$node['name']]->getCount() + 1
       
);
       
$count = $count + 1;
    }
}
foreach(
$tags as $tag)
{
   
$mycount = $tag->count;
    if(
$mycount > $threshold)
    {
       
$fraction = ((int)(($mycount / $count) * 60)) / 10;
        if(
$fraction < $font_size)
        {
           
$fraction = $font_size;
        }
        echo
'<span style="font-size: ' . $fraction . 'em;">' . l($tag->name,'taxonomy/term/' . $tag->tid,array('title="' . $tag->count . ' Nodes"')).'[' .$tag->count .']</span> ';
    }
}
?>

If you only want to display the tag cloud from one vocabulary you need to add this to the end of the query

$query = "SELECT d.name,d.tid FROM {term_data} d, {term_node} dn WHERE dn.tid=d.tid and d.vid='6';";

Where my vocabulary id is 6


WARNING

Don't try to use this twice on the same page by changing the VID, to have two or more tag cloud blocks, each showing different vocabularies.

This results in:
Fatal error: Cannot redeclare class tag in /home/website/public_html/includes/common.inc(1347) : eval()'d code on line 3

To correct this error, delete one of the two instances of these blocks from the database. Thanks to brewreview.info for this warning.

This is an example: Documentados
Oskar

About this page

Drupal version
Drupal 5.x, Drupal 7.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here