This article explains how to count the number of items in a category and display the result.


Let's say you have three categories:

  • Appples
  • Oranges
  • Pears

You want to display the categories and show how many articles are in the category.

You need to create a profile that uses all three categories and a style that has the following code:

The Code

# set the display text to null
$displayText = "";
# check to see if this is the first time through
if (not $firstPass{$ProfileName}) {
    $firstPass{$ProfileName} = 1;
    # save the value of the category into a variable to use later
    $currentCategory = $Category;
	# initialize a counter fo the total number of items processed
	$totalItems{$ProfileName} = 1;
} else {
    $firstPass{$ProfileName} = 2;
	# increment the total items counter
$profileCount =  $totalItems{$ProfileName};
# test to see if the itemCounter is already set
if ( (not $itemCount{$ProfileName} || $itemCount{$ProfileName} == 0) ) {
	$itemCount{$ProfileName} = 1;
} else {
    # increment the counter
#check to see if the category has changed or if we have reached the end of the last category
if ( (&isNewCategory && $firstPass{$ProfileName} != 1) ||
      ($totalItems{$ProfileName} == $NCTotalCount{$ProfileName}) ) {
    if ($totalItems{$ProfileName} != $NCTotalCount{$ProfileName}) {
	    #we counted the first item in the new category so we have to adjust the count
	#make the text agree with the number
    if ($itemCount{$ProfileName} == 1) {
        $lastWord = "item";
        } else {
        $lastWord = "items";
# set the text to display
$displayText = "The category $currentCategory has $itemCount{$ProfileName} $lastWord.";
# set currentCategory to the new category
$currentCategory = $Category;
# reset the counter to one - remember we have processed the first item of the new category already
$itemCount{$ProfileName} = 1;
</PerlCode><If: Field: displayText><h1><Field: displayText></h1>


The output produced will look like this:

<h1>The category Apples has 1 item.</h1>
<h1>The category Oranges has 3 items.</h1>
<h1>The category Pears has 26 items.</h1>


This style code will display the category name and the number of items in each category. It will not display a category that has zero items.

To work you need the Sort Orders addon (you must sort the profile by Category) and the NewsCounter addon (to tell when you have reached the end of the last category processed).

The if statement after the perl code begins on the same line as the closing perl code statement to prevent a bunch of blank lines in the created file. The closing if statement is on a new line so that there will be only one category's name and number of items on each line written to the file.

Remember if you cut and paste from here you may get extra line feeds and spaces that can cause syntax errors.

You could use this method to create a list like the one you linked to by changing the html that the category names and totals are wrapped in.

The style will work with multiple profiles and will count only the categories that are included in the profile being processed.

Further Discussion

This article is the result of this thread in the Coranto Support Forum.


Page last modified on February 02, 2009, at 07:23 PM