Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/10901-tag-clouds/
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.
Link to comment
https://forums.phpfreaks.com/topic/10901-tag-clouds/#findComment-40733
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.
Link to comment
https://forums.phpfreaks.com/topic/10901-tag-clouds/#findComment-40741
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]
Link to comment
https://forums.phpfreaks.com/topic/10901-tag-clouds/#findComment-40746
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.