Jump to content


Photo

Tag Clouds


  • Please log in to reply
6 replies to this topic

#1 sprinkles

sprinkles
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 31 May 2006 - 09:31 PM

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

#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 31 May 2006 - 10:25 PM

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.

NoGray.com


#3 sprinkles

sprinkles
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 31 May 2006 - 10:38 PM

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.

#4 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 31 May 2006 - 11:49 PM

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.
// 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;
     }
}

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

NoGray.com


#5 sprinkles

sprinkles
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 01 June 2006 - 12:18 AM

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


$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));

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


#6 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 01 June 2006 - 12:37 AM

my bad, I added an extra new in front of the array

try this
$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));

NoGray.com


#7 sprinkles

sprinkles
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 01 June 2006 - 01:00 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users