Help Modifiying Code for Tags

6 posts by 3 authors in: Forums > CMS Builder
Last Post: September 27, 2016   (RSS)

Hello, All -

I've been using some code to create a tag a cloud which works really well:

<?php
$tagsToCount = array();
$sectionsToField = array('records' => 'tags');
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]);
// 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;
      }
    }
  }
}
?>

<?php
//extract tags from the array
$tags = array_keys($tagsToCount);
$maximumResults = 20;
$resultCount = 0;
?>

It works very well. The only thing it doesn't do, however, is order the tags properly. What I would like to do is modify things so that the most commonly used tags appear first in the list.

I have no idea how to do this so could someone please point me in the right direction. 

:0)

Perch

By Damon - September 26, 2016

Hi,

I haven't setup the tag cloud with this code so I'm not sure but could you use PHP sort() ?

Cheers,
Damon Edis - interactivetools.com

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

Hi, Damon -

Thanks for your input but sort() won't work - not until I can first count the number of times each particular tag has been used.

How do I do that?!

:0)

Perch

Ah...!

Sorry Dave (and Damon!). I had hold of the wrong end of the stick. I see what the code does now and where I should add the sort(). I've dropped it in here:

<?php
$tagsToCount = array();
$sectionsToField = array('records' => 'tags');
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]);
// 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;
      }
    }
  }
}
?>

<?php
//extract tags from the array
arsort($tagsToCount);
$tags = array_keys($tagsToCount);
$maximumResults = 20;
$resultCount = 0;
?>

This gives the intended result.

Just one thing; the count is wrong - due to the Postincrement code further up the chain...

$tagsToCount[$tag]++;

This means the count is always one less than the actual number. I thought I might be able to correct this by using a Preincrement - but it doesn't work:

++$tagsToCount[$tag]

Any ideas?

:0)

Perch

By Dave - September 27, 2016

Hi Perch, 

Try replacing this: 

//turn field into an array of values
    $tags = explode("\t", @ $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;
      }
    }

With this:

  $tags = explode("\t", @$record[$fieldName]);    // to lowercase tags use: mb_strtolower($record[$fieldName])
  $tags = array_filter(array_map('trim', $tags)); // remove whitespace and empty tags
  foreach ($tags as $tag) { @$tagsToCount[$tag]++; }

Let me know if that works for you!

Dave Edis - Senior Developer
interactivetools.com