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

By Dave - September 27, 2016

Hi Perch, 

It looks like you've already got an array of tag names to counts called $tagsToCount.

PHP has a number of different sorting functions here: 
http://php.net/manual/en/array.sorting.php

I think the one you want is: arsort();

So if we start with this test data:

$tagsToCount = array (
  'apples' => 20,
  'oranges' => 33,
  'pears' => 2,
  'bananas' => 2,
  'peaches' => 22,
  'apricots' => 22,
);
arsort($tagsToCount);

// print output
header("Content-type: text/plain");
print_r($tagsToCount);
exit;

We get some output like this:

Array
(
    [oranges] => 33
    [peaches] => 22
    [apricots] => 22
    [apples] => 20
    [pears] => 2
    [bananas] => 2
)

Does that do what you need? 

And if you need it sorted by count and then alphabetic, try this:

krsort($tagsToCount);
arsort($tagsToCount);

Dave Edis - Senior Developer
interactivetools.com

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