You are here:  » Product Count Next to Category

Active Forum Topics

Product Count Next to Category

Submitted by noodles on Mon, 2006-09-04 16:21 in

Is there any simple way to show how many items are in each category? like this...

Electronics(315 products)


Submitted by support on Mon, 2006-09-04 17:28

Hi Dave,

It's pretty straight forward to add a count of each category - it just needs a minor change to the SQL, and a bit of code to add the count in brackets after the category name.

In categories.php, change:

$sql = "SELECT DISTINCT(category) as category FROM `".$config_databaseTablePrefix."products` ORDER BY category";

(line 6)


  $sql = "SELECT DISTINCT(category) as category,count(category) as count FROM `".$config_databaseTablePrefix."products` GROUP BY category ORDER BY category";

and change:

        $item["name"] = $product["category"];

(line 16)


        $item["name"] = $product["category"]." (".$product["count"].")";

That should do the trick. Similar modifications could be used in brands.php of course.

Hope this helps!

Submitted by noodles on Mon, 2006-09-04 19:10

That's perfect, many thanks.


Submitted by user101 on Tue, 2006-09-26 11:46

can we edit the output to


Electronics (315) or in HTML

Electronics (315)

Submitted by support on Tue, 2006-09-26 12:41


This can be done. It will involve creating a new parameter to pass through to the a2z HTML module, so modifications to both categories.php and html/atoz.php.

In categories.php change line 16 back to:

$item["name"] = $product["category"];

...and add a new line after that:

$item["extra"] = " (".$product["count"].")"

Then, in html/atoz.php, find the following code (line 43):

print "<p><a href='".$item["href"]."'>".$item["name"]."</a></p>";

...and replace it with the following code:

print "<p><a href='".$item["href"]."'>".$item["name"]."</a> ".$item["extra"]."</p>";

That should do the trick....


Submitted by philstone on Thu, 2014-01-30 09:47

Hi David

hope all is well

Is there a quick way to do this in the searchresults.php sidebar beside categories and brands?


Category name 1 (13)
Category name 2 (7)
Category name 3 (1)



Phil Stone

Submitted by support on Thu, 2014-01-30 12:03

Hello Phil,

Sure - your sidebar category A-Z code probably has a line to generate the SQL to select the categories, something like:

  $sql = "SELECT DISTINCT(category) FROM `".$config_databaseTablePrefix."products` WHERE category <> '' ORDER BY category";

If you have something (or very close to), REPLACE with the following:

  $sql = "SELECT DISTINCT(category) AS category,COUNT(id) as resultCount FROM `".$config_databaseTablePrefix."products` WHERE category <> '' GROUP BY category ORDER BY category";

Then your loop to display each category should begin something like:

  foreach($rows as $row)

Within this loop, you can use the variable $row["resultCount"] to show the number of results for each item, e.g.

  print "<li><a href='".$href."'>".$row["category"]."</a> (".$row["resultCount"].")</li>";

If you're not sure how to apply the above to your exact code, post the version of this mod that you're using and I'll work out the changes for you...


Submitted by philstone on Thu, 2014-01-30 15:12

Thanks again David

works a treat!!


Phil Stone

Submitted by stevebi on Wed, 2015-04-01 04:56

This is what I needed (ALSO :-) ) for my sidebar David!!!

Just to ask, before I apply it, is it compatible with my version?

Once more thank you for your support and thank you for this great script



Submitted by support on Wed, 2015-04-01 08:12

Hello Steve,

Thank you for your comments!

Your version would require the equivalent for category hierarchy, but I just looked at the last copy I have from you by email and it appears to have the category filter disabled (by way of an if(FALSE)....) so that may be out of date - please could you email me your latest version and let me know which filters you wish to include result counts for (or all of them if required) and I'll show you how to patch your version...


Submitted by stevebi on Wed, 2015-04-01 08:21

Surely David,

I am interested in adding the count filter at categories.php and brands.php.

I will send the files right away.

As you have mentioned me sometime at the past there might be a speed issue due to the count. Can we apply cache over the count?

Thanks again!!!


Submitted by smartprice24 on Thu, 2018-01-11 16:55

Hi David!

This featured is possible work in categories hierarchy mode?


Submitted by support on Fri, 2018-01-12 10:26

Hello Giuseppe,

Sure - to show product count in brackets next to leaf node categories in the dynamic drop down menu, edit html/menu_categories.php and look for the following code beginning at line 34:

        print "<li>";
        print "<a href='".$itemHREF."'>".$category["name"]." (".$rows4[0]["numProducts"].")</a>";
        print "</li>";

...and REPLACE with:

        $sql4 = "SELECT COUNT(DISTINCT(name)) AS numProducts FROM `".$config_databaseTablePrefix."products` WHERE categoryid='".$category["id"]."'";
        print "<li>";
        print "<a href='".$itemHREF."'>".$category["name"]." (".$rows4[0]["numProducts"].")</a>";
        print "</li>";