How to make a tag cloud in PHP, MySQL and CSS
Today I wanted to make a tag cloud, but all my searching proved fruitless - so I decided to stop being lazy and just work it out myself!
For my example, first you'll need a database that stores the keywords / search terms.
The way mine works is every time a search is run, I check to see if the term has been searched before. If it has, I update the counter field + 1. If the term has not been searched for before, I insert it with a counter of 1.
Alternatively you may store every search individually and use the GROUP BY clause to determine how many times the search has been run.
My query looks like this:
-
// don't forget to connect to DB first!
-
-
$maximum = 0; // $maximum is the highest counter for a search term
-
-
-
{
-
$term = $row['term'];
-
$counter = $row['counter'];
-
-
// update $maximum if this term is more popular than the previous terms
-
if ($counter> $maximum) $maximum = $counter;
-
-
-
}
-
-
// shuffle terms unless you want to retain the order of highest to lowest
Next we'll setup a bit of css. Feel free to adjust these as you see fit.
-
#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: x-small;
-
}
-
-
.small {
-
font-size: small;
-
}
-
-
.medium {
-
font-size:medium;
-
}
-
-
.large {
-
font-size:large;
-
}
-
-
.largest {
-
font-size:larger;
-
}
Finally we just want to loop through the array and display it with the appropriate css class.
-
// start the output to the page
-
echo "<h3>Popular Searches</h3>
-
<div id=\"tagcloud\">
-
<div>\n";
-
-
foreach ($terms as $k) // start looping through the tags
-
{
-
// determine the popularity of this term as a percentage
-
-
// determine the class for this term based on the percentage
-
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';
-
}
-
-
// output this term
-
}
-
-
// close the output
-
echo "</div>
-
</div>\n";
And thats it! Hope this saves someone some time. Add your comments below!
Just try placing tags around the css part, if that is where you are having your difficulties, then start the new blocks with the php open and close tags.
Hey Steve,
Thanks for your script. I am a newbie at php and I'm currently learning it so bare with me for the simple 'stupid' question yet important to me. I am trying to implement your script and I get this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING, could you direct me to what is wrong here and possibly a solution out of it?
Much thanks!
Hey Steve,
Thanks for the script! Implementation is really nice. Took me about 4 mins to get this up and running.
-Brandon
Hi Steve,
Been looking for something exactly like this! Seen a few examples online that over-complicated things, this one does what it says on the tin. Nice job.
Thanks alot for the script! Works flawlessly! I could easy combine it with an other script of mine.
Gr,
Anthony
Great job, it took me some time to understand but it worked like a charm.
Thanks
Thank you, Excellent piece of code..it took just few minutes to tag cloud up.
Nice example here Steve.
I've made a few modifications to my implementation of this, and have wrapped it all into an easy to use function.
Would it be OK if I share that function on my site? I would obviously make references to this post of yours.
no worries Tyler, you can reprint some/all of this code with acknowledgment.
Cool Steve. I'll let you know once it's published.
Steve, post is up:
http://www.longren.org/2010/02/24/how-to-build-a-tag-cloud-with-php-mysql-and-css/
You can delete this comment if you want, just wanted to let you know it was up and I don't have your email or any other way to contact you.