Author Topic: Image Count for Subcategory on Main Page  (Read 16832 times)

0 Members and 1 Guest are viewing this topic.

Offline Sebas Bonito

  • Sr. Member
  • ****
  • Posts: 271
  • Sebas Bonito
    • View Profile
Re: Image Count for Subcategory on Main Page
« Reply #15 on: July 14, 2009, 03:30:07 PM »
Has anyone figured this out yet?  I am having the same problem.  I created a subcategory and then several subcategories under that.  My total for the main subcategory is 0, since all the images are in the subcategory under that.  For example:

Category:           Zoo Special Events (5)
Subcategory:             Egg Day (5)
Subcategory:             Migratory Bird Day (0)
Sub-Subcategory:             Games (15)
Sub-Subcategory:             Art Show (20)

This is very confusing, because only the main and subcategory show up in the main categories page, so it looks like there are no images under Migratory Bird Day, when there are actually 35.  The user doesn't know to click on it and look for the subcategories since it says there are 0 images.

If this cannot be solved, is there an easy way to show more levels of subcategory on the main category list?

+++PUSH+++  :D

For the meanwhile I help me out with a trick:
REPLACE the whole $sub_cat_list Line with the following...

Code: [Select]
if ($cat_cache[$subcat_id]['num_images'] == 0) {
  $anzahl = "";
  }
  else {
  $anzahl = " (".format_text($cat_cache[$subcat_id]['num_images']).")";
  }
      $sub_cat_list .= "<a href=\"".$sub_url."\" class=\"subcat\">".format_text($cat_cache[$subcat_id]['cat_name'], 2)."</a>".$anzahl."";

It's not the best, but now it shows nothing, if there is really no content inside the subcategory OR if there is the 0-Bug.
Otherwise it will print out (5) for example.
« Last Edit: July 14, 2009, 03:42:38 PM by Schnick und Schnack »

Offline cc

  • Pre-Newbie
  • Posts: 2
    • View Profile
Re: Image Count for Subcategory on Main Page
« Reply #16 on: June 17, 2011, 11:11:02 AM »
I know this is a very old thread, but it looks like no-one has solved this issue? So I decided to share what I did to implement a way of showing counts for a category hierarchy that includes the total images in subcategories. However, I found that it increased the number of SQL statements executed per page from around 25 to over 800, thus slowing down my pages. This was for a hierarchy of 163 categories up to 4 levels deep.

It involves a recursive function that starts from the top level and for each category finds all of its children, and all of its children's children, and so on, to build up a subcategory list for each category, which is then used to query the image table to get the total hierarchy counts.

I put the recursive function in functions.php and call it from globals.php to update the cat_cache with the results. I had then to put a fix into page_header.php otherwise the total_images value will double/triple/etc count the images in subcategories.

However because of the performance hit I decided to implement this in a different way which works for my site but may not be appropriate for other sites (I use my site as a way of distributing my catwalk fashion images to publishing clients, so I am the only person who uploads images and I normally do this in bulk via ftp). I made a plugin that does the same recursive hierarchy count but stores the results in a new field added to the categories table for each category. The population of cat_cache in globals.php is now just a single query to retrieve these values. Every time I do a bulk upload I run this plugin.

HTH,
Rob

   

 

Post your comments here