sprinkles Posted May 31, 2006 Share Posted May 31, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/10901-tag-clouds/ Share on other sites More sharing options...
nogray Posted May 31, 2006 Share Posted May 31, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10901-tag-clouds/#findComment-40719 Share on other sites More sharing options...
sprinkles Posted May 31, 2006 Author Share Posted May 31, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10901-tag-clouds/#findComment-40723 Share on other sites More sharing options...
nogray Posted May 31, 2006 Share Posted May 31, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10901-tag-clouds/#findComment-40733 Share on other sites More sharing options...
sprinkles Posted June 1, 2006 Author Share Posted June 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10901-tag-clouds/#findComment-40741 Share on other sites More sharing options...
nogray Posted June 1, 2006 Share Posted June 1, 2006 my bad, I added an extra new in front of the arraytry 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] Quote Link to comment https://forums.phpfreaks.com/topic/10901-tag-clouds/#findComment-40746 Share on other sites More sharing options...
sprinkles Posted June 1, 2006 Author Share Posted June 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10901-tag-clouds/#findComment-40753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.