Tag Cloud

4 posts by 2 authors in: Forums > CMS Builder
Last Post: October 28, 2011   (RSS)

Re: [ryanGT] Tag Cloud

By Jason - October 27, 2011

Hi,

There are a number of different method for creating tag clouds. However, you first need to get an array of all of your "tags" along with a count of how frequently they occur.

In this example, you first create an array of all the sections you want to use and which field in each you tags are stored in. This script assumes that the tags in each field are separated by commas. It is also assumed that you are using CMSB version 2.08 or greater:

$tagsToCount = array();
$sectionsToField = array('reviews' => 'page_keywords', 'features' => 'page_keywords');

foreach($sectionsToField as $sectionName => $fieldName) {

//get section records
$records = mysql_select($sectionname);

foreach ($records as $record) {
//turn field into an array of values
$tags = explode(",", @$record[$fieldName]);

// add tags to the count array
foreach ($tags as $tag) {
$tag = trim($tag);
if (!$tag) { continue; } // skip empty array values

if (array_key_exists($tag, $tagsToCount)) {
$tagsToCount[$tag]++;
}
else {
$tagsToCount[$tag] = 0;
}
}

}

}


At the end of this code, you'll have an array called $tagsToCount where the index of an array element is the tag and the value is the number of times it appeared.

At the end of this you can use this code:
showme($tagsToCount);
to view the structure of the array.

Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Tag Cloud

By ryanGT - October 28, 2011

Hi Jason,

Thats exactly what i need, but instead of counting duplicates could the function remove duplicate values?

Also i am struggling with extracting the tags from the array, no matter what i do i seem to always just get the numerical values out of the array, needless to say php coding is not my strongest area.

Also when returning the results it would be really good to have the functionality to limit the number of results and display them in a random order.

Again your help would be greatly appreciated.

Many thanks

Ryan

Re: [ryanGT] Tag Cloud

By Jason - October 28, 2011

Hi Ryan,

When going through this array, each tag will only appear once, even if it's counted multiple times.

The problem you were probably encountering with getting the tags out of the array, is that the tags are actually the array indexes, not the values. You can try this after the previous code executes.

This example extracts all the tags into an array, randomizes the array, and then outputs the first 5 tags:

//extract tags from the array
$tags = array_keys($tagsToCount);

//randomize tags
shuffle($tags);


$maximumResults = 5;
$resultCount = 0;

foreach($tags as $tag) {

echo $tag;

if (++$resultCount == $maximumResults) {
break;
}

}


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/