TagCloud Goodness
p. There is no more perfect icon of Web 2.0 than the TagCloud. These days they are everywhere and while some see them as the new pink, they are still a valid navigational element. Jenny, (last name to be determined) over at “Prism-Perfect”:http://www.prism-perfect.net/ shows a quick way to build them.
p. This is not so much a tutorial as it is a code dump. The article, inappropriately titled “PHP Tag Cloud Tutorial”:http://www.prism-perfect.net/archive/php-tag-cloud-tutorial/, contains a grand total of words of non-code text. Here, I’ll give them to you.
bq. Well, it’s actually so simple it’s not really a tutorial at all so much as a snippet with a bit of explanation. But I’ve had some people ask how it’s done, so here’s how I do it. I tried to go through and add some comments about what’s happening.
p. Now, if you are not interested in reading the code, there’s really no need to visit the page. However, I’m guessing most of you want the code. The rest of the blog post is a code listing for her solution for building TagClouds. It’s al procedural code but it’s a solid solution that should be pretty easy to implement. It’s also nice to see decent commenting in tutorial code.
p. If you are looking for a more verbose, (400 lines compared to Jane’s 60) OO and not necessarily better way to do it, I wrote a TagCloud class a while back. You will find code and an example of it’s use on “this page”:http://www.calevans.com/view.php/page/tagcloud.
p. =C=


2 comments to “TagCloud Goodness”
August 23rd, 2006 at 8:48 am
I don’t really see what value is added by this unnecessary complexities.
The following code displays a perfectly fine, CSS-styled tag cloud:
== in controller: ==
// parameters passed: tag number $limit and number of $levels
// using Zend_Db
$select->from(‘presentations’, ‘tags’)->where(‘tags IS NOT NULL’)->where(‘active’);
$tags = array();
// split by commas and populate the $tags array
foreach($db->fetchCol($select) as $keywords)
$tags = array_merge($tags, preg_split(‘/\s*,\s*/’, trim($keywords), null, PREG_SPLIT_NO_EMPTY));
// count the tag frequency
$tags = array_count_values($tags);
if(($count = count($tags)) > $limit){
arsort($tags, SORT_NUMERIC);
$tags = array_slice($tags, 0, $limit);
}
$max = max($tags); $min = min($tags);
$ratio = ($max – $min)/$levels;
foreach($tags as $tag => $freq)
$tags[$tag] = $freq == $max ? $levels : ceil(($freq – $min + 1) / $ratio);
ksort($tags);
== in the view: ==
<? foreach($this->tags as $tag => $lev)
$mytags[] = sprintf(‘<a href="%s" class="taglev%d">%s</a>’,
$this->link(‘search/tag/’ . htmlspecialchars($tag)), $lev, htmlspecialchars($tag));
echo implode(‘, ‘, $mytags) ?>
== generated output: ==
<a href="/search/tag/A-V" class="taglev2">A-V</a>, <a href="/search/tag/aerobic" class="taglev1">aerobic</a>, <a href="/search/tag/alati" class="taglev1">alati</a>, …
== it looks like: ==
http://odin.irb.hr/~mislav/tmp/tags.png
The font sizes aren’t hardcoded so the cloud is easily styled with CSS.
August 23rd, 2006 at 9:02 am
Mislav. That’s a very nice looking TagCloud and a cool piece of code. Thanks for commenting.
=C=