Creating a Menu for Your Tags

I was working on a link sharing site, and I decided to move the tags out of a box on a sidebar and into a menu to salvage some width for the content. Moving tags away from the side required a visual cue to show how often tags are being used. This example assumes that you have created a "tags" table that is associated with your blog entries or links via a shared column. When I create a link for this site, I tag it and insert them into the tags table with the "id" of the link/entry being inserted at the same time. So now that I have the tags associated, I needed to call them out to the menu that I wanted to generate and dynamically change as people tag links.

function tagsmenu() {

// Before the query I manually add a link to View All Tags
echo "<li><a href="tags.php">View All Tags</a></li>";

// This is the query that gets the tags [SELECT tag], counts them up [SELECT COUNT(id)], 
// and creates the total occurrence of each tag.
// Then I limit the menu to only show the top 10 most used
$select = mysql_query( "SELECT tag, COUNT(id) FROM tags GROUP BY tag ORDER BY COUNT(id) DESC, tag ASC LIMIT 10" ) or die("SELECT Error: ".mysql_error());

while ($row = mysql_fetch_array($select)) { 
if ($rowtags['tag']!= "") {     
echo "<li><a href="tag-sort.php?name=" . $row['tag'] ."">";
echo "" . $row['tag'] . " (" . $row['COUNT(id)'] . ")</a></li>";                    

This generates the top 10 tags in use across the site by adding <pre> <? tagsmenu(); ?> </pre>to your templates. VIEW LIVE EXAMPLE Once you have this menu built, you can then create a tag-sort.php file that will handle the tag and will display the results that have been tagged with the item in the menu.
Post a comment

Real Time Web Analytics ^