Tag Cloud - need help

22 posts by 5 authors in: Forums > CMS Builder
Last Post: April 26, 2013   (RSS)

By gregThomas - April 3, 2013

Hi Jeremy,

This is great! Thanks for sharing it with us.

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Mikey - April 26, 2013

Great Tag Cloud! I needed something like this a few years ago. Glad to see one developed now.

So here's my little spin on it... which allows for multiple tags assigned to a single record, along with a little CSS to get the style going.

<style type="text/css">
/*----------------------------
Tag Cloud
----------------------------*/
#tag-cloud {
color:#063;
font-style:italic;
}
#tag-cloud a {
color:#063;
text-decoration:none;
font-style:italic;
}
#tag-cloud a:link {
color:#063;
text-decoration:none;
}
#tag-cloud a:hover {
color:#C00;
text-decoration:none;
}
#tag-cloud:hover {
color:#C00;
text-decoration:none;
}
</style>

<?php /* BEGIN TAG CLOUD */ ?>
<?php
$tagsToCount = array();
$sectionsToField = array('blog_content' => 'tags'/*, 'blog2_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("\t", @$record[$fieldName]); // Modified for multiple tags associated with record.

// 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[$key] = $list[$key]; 
  }
  return $random; 

?>

<?php /* DISPLAY TAGS */ ?>

<?php
 $tagsToCount = shuffle_assoc($tagsToCount); ?>
 <?php if ($tagsToCount): ?>
        <aside id="tag-cloud"> <?php /*?> Modified to assign css style <?php */?>
     <h3>Tags</h3>
         <?php  $maximumResults = 18;
$resultCount = 0;
?>
 
<?php $totalTagCount = array_sum($tagsToCount); ?>

<?php
$min_size = 10; //smallest font size
$max_size = 27; //largest font size
$minimum_count = min(array_values($tagsToCount));
$maximum_count = max(array_values($tagsToCount));
$spread = $maximum_count - $minimum_count;
if($spread == 0) {
$spread = 1;
}
    arsort($tagsToCount);
            $tagsToCount = array_slice($tagsToCount, 0, 17);
            $tagsToCount = shuffle_assoc($tagsToCount);
foreach ($tagsToCount as $key => $value): ?>

<?php           
$size = $min_size + ($value - $minimum_count) * ($max_size - $min_size) / $spread;
?>

<a style="font-size: <?php echo floor($size)?>px;" href="../blog_list.php?tags_keyword=<?php echo $key;?>" title='Articles: <?php echo $value; ?>'><?php echo $key; ?></a>
              <?php if (++$resultCount == $maximumResults) { break;}?>
  <?php endforeach ?>
        </aside>
<?php endif ?>
<?php /* END TAG CLOUD */ ?>