Tag Cloud and Categories of Records

2 posts by 2 authors in: Forums > CMS Builder
Last Post: February 10, 2022   (RSS)

By Mikey - February 5, 2022

Howdy folks,

Below is some code that loads a Tag Cloud that I use to query a site's search engine. In my news, photos, videos and events section editors I a pulldown called "categories" that I can assign a category to each record I create. I'd like to load up all the chosen categories in the tag cloud, but I can't figure out what needs to be done to load up the select for 'categories:label' in the code below. Any suggestions on how to get this to work?

$tagsToCount = array();

$sectionsToField = array(
   'news'      => 'categories:label',
   'photos'    => 'categories:label',
   'videos'    => 'categories:label',
   'events'    => 'categories:label',
);

//use additional if joining tags from multiple sections
foreach($sectionsToField as $sectionName => $fieldName) {

//get 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);  // Disable to show largest quantity at the top and smallest at the bottom.
  $random = array(); 
  foreach ($keys as $key) { 
    $random[$key] = $list[$key]; 
  }
  return $random; 
} 
?>

<!-- DISPLAY TAGS -->

<?php $tagsToCount = shuffle_assoc($tagsToCount); ?>
 <?php if ($tagsToCount): ?>
         <?php  $maximumResults = 32;
$resultCount = 0;
?>
 
<?php $totalTagCount = array_sum($tagsToCount); ?>

<?php
$min_size = 13; //smallest font size
$max_size = 22; //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, 99);
            $tagsToCount = shuffle_assoc($tagsToCount);
foreach ($tagsToCount as $key => $value): ?>

<?php $size = $min_size + ($value - $minimum_count) * ($max_size - $min_size) / $spread; ?>
    <a href="search.php?q=<?php echo $key;?>" class="button small hollow" style="font-size: <?php echo floor($size)?>px;" title='<?php echo $key; ?>'><?php echo $key; ?></a>
<?php if (++$resultCount == $maximumResults) { break;}?>

  <?php endforeach ?>
<?php endif ?>
<div class="clearfix"></div>
<!-- END TAG CLOUD -->

Thanks Zicky