Jump to content

Archived

This topic is now archived and is closed to further replies.

sprinkles

Tag Clouds

Recommended Posts

Hello,
I need to tag clouds for a site I am working on, but I can't quite get my head around it!

I've found a code snippet that distribues the font sizes, but that does not include how to count the tags in the first place to get it to work! (the snippet is this one [a href=\"http://www.hawkee.com/snippet.php?snippet_id=1485)\" target=\"_blank\"]http://www.hawkee.com/snippet.php?snippet_id=1485)[/a]

In my database I have the tags in a field seperated by spaces for each record (I know this was a bad way of doing it, but it can't be changed now). I have no problem taking all the tags from the database, and putting them into an array, but after this, I can't work out how to count the tags and turn it into a tag cloud!

So I have an array full of tags, but I can't work out how to do anythign with it!

Any help/ideas appreicated!

Richard John

Share this post


Link to post
Share on other sites
When you create the array, use the tag as the index and the count as the value (arr['tag'] = 5) than you can get the count for each tag by calling the arr['tag'] for it.

Share this post


Link to post
Share on other sites
Thank you for the reply.

How do I actually get the count, though?

The tags have not been counted, there are just multiple instances of them in the array.

Share this post


Link to post
Share on other sites
let's assume your array is called $tags_arr, you'll need to go through it and create a new array that hold the count.
[code]
// assume your array is $tags_arr

$tags_count = new array();

foreach ($tags_arr as $tag){
     if (array_key_exists($tag, $tags_count)){
          $tags_count[$tag]++;
     }
     else {
          $tag_count[$tag] = 1;
     }
}
[/code]

this will give you a new array ($tag_count) with the tag as the index and the count as the value.

Share this post


Link to post
Share on other sites
Using this code (so I can see the array) gives me the error:
Parse error: parse error, unexpected T_ARRAY, expecting T_STRING or T_VARIABLE or '$' in /home/radioone/public_html/tagsave.php on line 7

[code]

$tags_arr = explode(",",$tags);


$tags_count = new array();

foreach ($tags_arr as $tag){
     if (array_key_exists($tag, $tags_count)){
          $tags_count[$tag]++;
     }
     else {
          $tags_count[$tag] = 1;
     }
}

print_r(array_keys($tags_count));[/code]

The variable "tags" contains all of the tags seperated by commas.

Share this post


Link to post
Share on other sites
my bad, I added an extra new in front of the array

try this
[code]
$tags_arr = explode(",",$tags);


$tags_count = array();

foreach ($tags_arr as $tag){
     if (array_key_exists($tag, $tags_count)){
          $tags_count[$tag]++;
     }
     else {
          $tags_count[$tag] = 1;
     }
}

print_r(array_keys($tags_count));
[/code]

Share this post


Link to post
Share on other sites
I think it's the code snippet on the Hawkee site that's confusing me. I just can't get my head around the way the array is supposed to be structured for it.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.