Tag Cloud, an add-on for the tags mod

Tag Cloud, an add-on for the tags mod

Be sure to check out the improved tag cloud first!

This is a tag cloud function I whipped up this afternoon. I did this very quickly, so I'm sure it could be improved upon. Copy the function below to your snews.php file, just above the closing tag.

You can change the minimum and maximum percentages if you like. To use, you simply call the function in your index.php like this;


<?php echo get_tagcloud(); ?>

or if you want to call it from inside an article, extra, or page, you insert it like this;

[func]get_tagcloud:|:[/func]

If you're using my tags mod version 1 and want to continue using that, simply replace the highlighted text with ?search_query= instead of tag/

function get_tagcloud() {
    $max_size = 300; // max font size in %
    $min_size = 100; // min font size in %
    $sql = "SELECT keywords_meta FROM "._PRE."articles WHERE date <= NOW() AND published='1' AND visible = 'YES' AND keywords_meta != ''"; 
    $result = mysql_query($sql);
    $count = mysql_num_rows($result);
    $x = "1";
    while ($row = mysql_fetch_array($result)) {
        if ($x < $count) {
            $suffix = ", ";
        } else {
            $suffix = "";
        }
        $tagslist .= $row['keywords_meta'].$suffix; 
    $x++;
    }

    $tagslist = explode(", ",$tagslist);
    asort($tagslist);
    $tagslist = array_count_values($tagslist);
    
    $max_occur = max(array_values($tagslist));
    $min_occur = min(array_values($tagslist));
    $range = $max_occur - $min_occur;
    if ($range == 0) { 
        $range = 1;
    }
        
    $step = ($max_size - $min_size) / ($range);
    echo '<div class="tagcloud"><p>';
    // loop through the tag array
    foreach ($tagslist as $key => $value) {
        $fontsize = round($min_size + (($value - $min_occur) * $step));
        echo '<a href="'._SITE.'tag/'.urlencode(strtolower($key)).'/" style="font-size: '.$fontsize.'%" title="'.$value.' items tagged with '.$key.'">'.$key.'</a> ';
    }
    echo '</p></div>';
}

Everything is contained in a div with the class of "tagcloud", so you can add that to your stylesheet. Here is a demo of the output from my site

[func]get_tagcloud1:|:[/func]

Tags

 

You might like

Comments


wow, well done Matt !

It looks very good, now I only need more "spare time" to be able to modify my sNews with all your mods ;)

Your mods are useful and really improve sNews, thanks again Matt


Thanks Sven, I appreciate the comments from yourself and others, makes it worthwhile.


I'm a fan now.
That one is the ultimate Tag Clod Mod for sNews. ;-)

One thing, i think tags links should not be adjacent, so ine the last echo it needs a | to separate links:
| ';">

Am I wrong?


Hi Philippe,

Yes, you could do that if you like, I think it might get a little cluttered however, so I just use the css margin attribute to keep the links from crowding each other.

Like this;


.tagcloud a {
margin:2px 4px 2px 0;
}


--Matt

Comments are closed. No new comments allowed.

Copyleft 2002 - 2017 Matt Jones
Hand crafted with HTML5 & CSS3
↑ Back to top