A popular tags list can help your users find articles relating to certain topics without having to guess what a good search input might be. This is the code I created for my own site, which now contains one such list.
First, you have to make sure your content is tagged appropriately. That means when it's uploaded to the database, it also has a tag or keyword section in each row. In my case, I accessed the database with this entry:
// Replace these values with your own.
$result = @mysql_query('SELECT keywords FROM yourtable ORDER BY ID DESC');
Notice I'm only taking the keywords from the postings table. That's all I need. No point in extracting extra information from the table if it's not going to be used.
Next comes the PHP. You can copy and paste this code, which should work for anyone who has the proper field in their database.
$listarray = array();
while($row = mysql_fetch_array($result))
// Replace 'keywords' with the name of the field in your database
// and use whatever delimiter you are using to separate the names
// in your field. In my case, it's a comma.
$rowarray = explode(',', $row['keywords']);
// Set a loop, assign the keyword to a variable
foreach($rowarray as $rowkeyword)
// Set the trigger in case it gets to the end without setting count
$notfound = true;
// Set a loop, assign keyword and counts as variables in each iteration
foreach($listarray as $listkeyword => $count)
// If the keyword supplied matches this iteration, add 1.
if($rowkeyword == $listkeyword)
$listarray[$listkeyword] = $count;
$notfound = false;
// If you've gone through the whole listarray and not found one, make one
$listarray[$rowkeyword] = 1;
I'd recommend reading the code and its comments to understand the process, but here's a quick rundown: It's creating an array that will eventually contain indexes that are the names of the various keywords in your database, and their values will be the number of times those keywords appear in your database, by way of the following steps:
What you do at this point is entirely up to you. If you want an example, here's what I did:
// Sorts array by its values, in ascending order, then reverses the order,
// both while maintaining the indices.
$listarray= array_reverse($listarray, true);
$i = 1;
$keywordstring = '<ul>';
foreach($listarray as $keywordlink => $appearances)
// Outputs HTML of array in list form, then echoes it.
$keywordstring .= '<li><a href="/home.php?cmd=topic&keyword=' . $keywordlink . '" class="bodyLink">' . $keywordlink . ' </a><span>' . $appearances . '</span>';
// Up to 10 keywords. You can set this as high as you want, though
// you should probably limit it, unless you're using a cloud of tags
// or some other format that would benefit from seeing all keywords.
if($i > 10)
$keywordstring .= '</ul>';
If you opt to sort the array by the counts, make sure you use asort(). Regular sort() will replace your keywords. Also, this sets it in ascending order, so if you want, instead, for the array to go in descending order, use the array_reverse() function. You won't be able to put the asort() function inside of the array_reverse() function as it doesn't return a variable. It simply changes the variable sent to the asort() function. Also, make sure you set the second value in array_reverse() to 'true', otherwise, it will reset your indexes to numbers.
The rest is pretty standard. Design it to your liking. The key thing here is that, after the first process, you'll have a clean list of keywords and the number of times they appear. You can set an algorithm that figures out the max iterations versus the minimum iterations and dynamically generates the style of the final output. You know, if you really wanted to.
If you have any questions, feel free to e-mail me.
Some other articles you might enjoy...
|How to Talk Like PHP: Part 2||Monday, June 15, 2009|
|How to Talk Like PHP: Part 1||Tuesday, June 9, 2009|
|Snazzy jQuery Form Alert Boxes||Tuesday, February 9, 2010|
|E-mail Newsletter Form with jQuery, AJAX, MySQL and PHP||Sunday, March 14, 2010|
|Creating An Image Management System for Your Website with Smart Image Resizer and Lightbox 2||Saturday, July 11, 2009|