'tags'/*, 'testimonials_content' => 'tags'*/); //use additional if joining tags from multiple sections
function createTagCloud($tags)
{
//I pass through an array of tags
$i=0;
foreach($tags as $tag)
{
$id = $tag['id']; //the tag id, passed through
$name = $tag['tag']; //the tag name, also passed through in the array
//using the mysql count command to sum up the tutorials tagged with that id
$sql = "SELECT COUNT(*) AS totalnum FROM health_articles_content WHERE tags LIKE '%".$id."%' AND published = 1";
//create the resultset and return it
$res = mysql_query($sql);
$res = mysql_fetch_assoc($res);
//check there are results ;)
if($res)
{
//build an output array, with the tag-name and the number of results
$output[$i]['tag'] = $name;
$output[$i]['num'] = $res['totalnum'];
}
$i++;
}
/*this is just calling another function that does a similar SQL statement, but returns how many pieces of content I have*/
$total_tuts = $this->getNumberOfTutorials();
//ugh, XHTML in PHP? Slap my hands - this isn't best practice, but I was obviously feeling lazy
$html = '
';
//iterate through each item in the $output array (created above)
foreach($output as $tag)
{
//get the number-of-tag-occurances as a percentage of the overall number
$ratio = (100 / $total_tuts) * $tag['num'];
//round the number to the nearest 10
$ratio = round($ratio,-1);
/*append that classname onto the list-item, so if the result was 20%, it comes out as cloud-20*/
$html.= '- '.$tag['tag'].'
';
}
//close the UL
$html.= '
';
return $html;
}
?>
'tags'/*, 'testimonials_content' => 'tags'*/); //use additional if joining tags from multiple sections
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] = 1;
}
}
}
}
//PHP doesn't have a shuffle function built in that maintains keys, but this version does.
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[] = $list[$key];
}
return $random;
}
showme($tagsToCount);
?>