Fun Infused Games  |   Smooth Operator

  Home   |    Archive   |    About
Posts prior to 8/2/2010 may be missing data. If you need one of those posts, please contact kriswd40@yahoo.com and I will try and recover/find it.

Creating A List Of Unique Tags For Your Site Part 2
Date 6/24/2008    Tags C#, ASP.net    (0)

A couple of weeks ago I posted an article about how to create a list of unique tags for your website. While the initial tutorial was all fine and dandy, I felt like there were still some things I could improve. First and foremost, a trim command needs to be done to the tags, otherwise it has treated tags with beginning or trailing spaces as different tags. Secondly, one of our readers suggested that instead of using random font sizes, I should make the tags progressively bigger the more times the tag occurred. I'm not going to exactly do that, what I am going to do is add the count of stories that belong to a tag after said tag is listed. Finally, I will setup alternating colors for the tags so that they are easier to read.

If you are following along to create this set of tags on your own website, you will need to start with part 1 since we will be building and modifying the code from that example.

First let's began with the trim function, which is easily taken care of simply by adding the .Trim() command when traversing the FOR loop and adding your tag.

    private void AddTags(string blogTags)
    {
        char[] seperator = { ',' };

        Array blogTagSplit = blogTags.Split(seperator);

        foreach (string blogTag in blogTagSplit)
        {
            // Only add new tags, no duplicates
            if (!blogTagArray.Contains(blogTag.Trim()))
            {
                blogTagArray.Add(blogTag.Trim());
            }
        }
    }
Next, we must modify the LoadTags() funtion so that we know how many of each tag we have. In the first article we used an ArrayList to load all the tags. For this go around, since we also need to load the number each time a tag occurs, we will modify our function to use a HashTable. Declare a globally accessible HashTable like so...

private Hashtable blogTagHashTable = new Hashtable();
And modify the LoadTags() function loop through your hashtable and create new key/value pairs for those that don't exist and add to those who do.

    private void AddTags(string blogTags)
    {
        char[] seperator = { ',' };

        Array blogTagSplit = blogTags.Split(seperator);

        foreach (string blogTag in blogTagSplit)
        {

            if (blogTagHashTable.ContainsKey(blogTag.Trim()))
            {
                // Get value of hash key and add one
                int tagCount = Convert.ToInt32(blogTagHashTable[blogTag.Trim()]) + 1;
                blogTagHashTable[blogTag.Trim()] = tagCount.ToString();
            }
            else
            {
                // Create new entry
                blogTagHashTable.Add(blogTag.Trim(), 1);
            }
        }
    }
Now that we've loaded the hashTable, we need to loop through and display the items in the hashtable. In this loop, we will also alternate between displaying one class and another for the links, giving us an alternating color scheme that makes the tags much more readable.

    protected 
                    

This article has been view 388 times.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*
Comment *
Insert Cancel
Things To Click


Tags
Video Games (7)  Trivia or Die (3)  SQL (1)  iOS (3)  Game Dev (11)  Advise (14)  PC (1)  World of Chalk (2)  FIN (20)  Abduction Action! (27)  XBLIG (32)  Abduction Action (1)  Nastier (4)  ASP.net (18)  Absurd (2)  Volchaos (11)  Web (19)  Fin (1)  XNA (40)  Rant (50)  Cool (2)  Visual Studio (1)  Trivia Or Die (1)  Xbox (1)  C# (14)  Sports (11)  Design (2)  Development (13)  Hypership (28)  WP7 (8)  VolChaos (1)  Nasty (34)  Abdction Action! (1)