Archive for the ‘PHP’ Category

How To: Build a Tag Cloud with PHP, MySQL, and CSS

3

Tag clouds are everywhere. They are a popular way to show the weight (read: popularity) of tags, categories, or any words really. I needed to build a tag cloud for a project at work to display categories and show how many questions were contained inside each category. Categories with more questions would need a larger font, and categories with fewer questions would need smaller fonts. I came across this post from Steve Thomas titled How to make a tag cloud in PHP, MySQL and CSS.

Steve’s code is exactly what I was looking for. I made a few modifications to his code and wrapped it all into a custom PHP function, which you can see below.

Download The Code

The function returns an array that contains each category name, the CSS class that should be applied to the given category, and the category ID for linking to the page showing only questions in that category.


<?php
function tagCloud($maximum=0) {
	$cats = array(); // create empty array

	$query = mysql_query("SELECT categories.catDesc, categories.catId, COUNT( questions.id ) AS totalQuestions FROM questions, categories WHERE questions.categoryId = categories.catId GROUP BY categories.catId;");
	while ($row = mysql_fetch_array($query)) {
		$cat = $row['catDesc'];
		$counter = $row['totalQuestions'];
		$catid = $row['catID'];

		// update $maximum if this term is more popular than the previous terms
		if ($counter> $maximum) $maximum = $counter;

		$percent = floor(($counter / $maximum) * 100);
		if ($percent <20) {
			$class = 'smallest';
		}
		elseif ($percent>= 20 and $percent <40) {
			$class = 'small';
		}
		elseif ($percent>= 40 and $percent <60) {
			$class = 'medium';
		}
		elseif ($percent>= 60 and $percent <80) {
			$class = 'large';
		}
		else {
			$class = 'largest';
		}
		$cats[] = array('term' => $cat, 'class' => $class, 'catid' => $catid);

	}
	return $cats;
}
?>

You’ll also need some CSS to style your tag cloud. This CSS is pretty much identical to what Steve published, I only made some minor changes to font sizes.


#tagcloud {
    width: 300px;
    background:#FFFFCC;
    color:#0066FF;
    padding: 10px;
    border: 1px solid #FFE7B6;
    text-align:center;
}

#tagcloud a:link, #tagcloud a:visited {
    text-decoration:none;
}

#tagcloud a:hover, #tagcloud a:active {
    text-decoration: underline;
    color: #000;
}

#tagcloud span {
    padding: 4px;
}

.smallest {
    font-size: 10px;
}

.small {
    font-size: 12px;
}

.medium {
    font-size:14px;
}

.large {
    font-size:16px;
}

.largest {
    font-size:18px;
}

To use the function, do something like this:


<?php $tagCloud = tagCloud(); foreach ($tagCloud as $t) { $cat = $t['term']; $class = $t['class']; $catid = $t['catid']; print "<span class=\\"$class\\"><a href=\\"category.php?id=$catid\\">$cat</a></span>"; } ?>

I am still testing this code, but so far it seems to do exactly what I need it to do. It’s best to use tagclouds in a virtual private server provider that supports PHP, MySQL and CSS. If you experience trouble with the code or if it doesn’t act as expected, please let me know in the comments. If you’ve got ideas on how to improve the function, I’d love to hear your thoughts as well!


WordPress Theme: Unwakeable 1.5.3

14

Unwakeable 1.5.3 is available for download. This version is built off K2 1.0.3 and should work beautifully with WordPress 2.9+. You can head over to the Unwakeable page to get the download, or you can grab it here.

K2 1.0 added more support for WordPress 2.9. For example, K2 1.0 supports new WordPress features such as post thumbnail images. One of the more noticeable changes to K2 since the 1.0 release is the absence of SideBar Manager (SBM). It sounds like it was simply too much work to maintain SBM, and was beyond the scope of what K2 is:

It’s worth mentioning that the last remnants of the old SideBar Manager, or SBM, have now been removed from the codebase. It started out as a fully-fledged replacement for WP’s lacking widgets system and ended up as a patch-of-sorts to the widget system, allowing for widgets to be placed only on specified pages. But in the end, while the native widget system is still very much in need of an update, it didn’t feel right for K2 to try and cover that particular area of the administration interface. And besides, other plugins for doing just that exist already.

So instead of spending our time patching that system for an ever-changing WordPress, our time is probably better spent on more theme-specific functionality, like the rolling archives or livesearch systems, as well as keeping up with new WordPress features, like for instance Post Thumbnails.

Rather than break down all of what’s changed in recent versions of K2, I’m going to make it easy on myself and direct you to the K2 1.0 release announcement. Just know that Unwakeable 1.5.3 sports all the features found in K2 1.0.3. You can comment on this post or on the Unwakeable page with questions or comments.


K2 1.0-RC8

3

K2 1.0-RC8 has been released. It has support for threaded comments and child themes. You can also edit the post meta without having to modify any code now. A list of bug fixes can be found here, and below is the full post from the K2 blog:

This release adds support for Threaded Comments and Child Themes. Styles have been improved. You can specify where Styles are stored at, activate multiple styles, and edit them in the WordPress Theme Editor. You can also now customize the Post Meta (the line that reads Published by John Doe…) without having to edit any code.


I plan on building the next version of Unwakeable in the next couple weeks and will likely base it off of K2 1.0-RC8.


Unwakeable For WordPress 2.7.1

4

I just uploaded a new version of Unwakeable, version 1.5.3 rc1. This new version works with WordPress 2.7.1, unlike the previous version of Unwakeable.

Unwakeable 1.5.3 rc1 is up-to-date with K2 r776. K2 no longer includes the K2 Sidebar Manager. If you were using K2 Sidebar Modules and want to continue using it, you can download the plugin here.

There’s a new option on the Unwakeable Options page that allows you to customize top and bottom meta information for each post. The top meta information will be displayed directly under the title of a post. The bottom meta information will be displayed at the end of the posts content. You can use the following keywords to define what information you want to show: %author%, %categories%, %comments%, %date%, %tags% and %time%.

Have a look below to see what to enter in the top meta and bottom meta sections to display the same information that’s being displayed here.
Top Meta: Published by %author% on %date% at %time% %comments%
Bottom Meta: Categories: %categories% %tags%

You can download Unwakeable 1.5.3 rc1 from the Unwakeable page, or click here to download it directly.


WordPress 2.7 and Unwakeable

8

WordPress 2.7 “Coltrane” is out. The new dashboard is a nice change, it makes getting around much easier. Here’s a bit from the release announcement:

The first thing you’ll notice about 2.7 is its new interface. From the top down, we’ve listened to your feedback and thought deeply about the design and the result is a WordPress that’s just plain faster. Nearly every task you do on your blog will take fewer clicks and be faster in 2.7 than it did in a previous version.

The latest stable version of Unwakeable, 1.5.1, seems to work just fine with WordPress 2.7, from what I’ve seen anyway. If you have any problems with Unwakeable 1.5.1 with WordPress 2.7, you may want to give Unwakeable 1.5.2 release candidate 1 a shot. As of this post, it’s current with K2 r739. Unwakeable 1.5.2 rc1 is currently running this site and appears to be very stable. I hope to have Unwakeable ready for inclusion in the WordPress theme directory by the time 1.5.2 is officially released.

If you have any problems with either of these versions of Unwakeable, please let me know by leaving at comment at the Unwakeable page.


Post navigation