Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

count number of sub_categories

Quote Reply
count number of sub_categories
I searched the forum and never found the code for this. I would like to count the number of sub categories on my category pages. If this global is in this forum, I could not find it...

I can figure out how to write it, but its time consuming...

Here is what I have...

Code:

sub {
#---------------------------------------------------
# Display the number of sub categories

my $tag = shift;

my $title = $tag->{title};




my ($db, $sth, $names);
my $count;
my $title = "Magic Directories/Dealers";
$db = $DB->table("Category");
$db->select_options("ORDER BY Name");
$sth = $db->select( ["Full_Name"] );
$names = $sth->fetchrow_array;

# run a loop and $count++;

# somehow cross reference the $title with the category names and count them...

return $name;
}


I haven't tested any of this, but I image it would be something close to this. It seems like a lot of work for something so simple...

Any ideas?

- Jonathan
Quote Reply
Re: [jdgamble] count number of sub_categories In reply to
If you are looking for the number of subcategories within a category, not all the subcategories below, you can look at the $category_loop variable on that page. That is an array that contains each of the sub categories for that page (1st level subcats)

sub {
my @num_cats = @{$_[0]->{'category_loop'}};
return scalar @num_cats
}

just call it as <%num_subcats%> or whatever you named it.

If you are trying to find subcats as well, check out the Yahoo Subcats plugin for some example code.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] count number of sub_categories In reply to
Thanks I was thinking too hard. I had to change the code a little. Correct me if I'm wrong, but since its an array it starts at 0.. therefore I subtracted one...


sub {
my @num_cats = @{$_[0]->{'category_loop'}};
my $cats = scalar @num_cats;
$cats--;
return $cats;
}

for some reason "return $cats--;" would not subtract one... weird...

Anyway... I new there was a simple way.

Thanks again,

- Jonathan
Quote Reply
Re: [jdgamble] count number of sub_categories In reply to
No,
Don't subtract one.
We are not asking for the last subscript, eg : $#array
What the "scalar" function does is force the array (a list context) into a scalar context, which returns the number of elements in the list, rather than the list. The scalar of a list is the # of elements, that isn't zero based. A list of (1) has 1 element. @lst = (1, 2, 3) has three elements. $#lst is 2, print $lst yeilds 3.
If you use the other functions, you need to ADD one, since they are zero based, eg: if the list has 20 elements, the highest element would be $array[19]

@{$_[0]->{'category_loop'}}

This is forcing the array_ref (category_loop) into an array context. There are more obscure ways of doing this, but I figured 2 lines for something this funky was sufficiently small.

$_ is the passed arguments, and [0] is the fact that the "$tags" hash is passed as the first parameter when no other args are present, and we are looking for the <%category_loop%> element of the $tags hash.


BTW: This "trick" is only available because links already "counts" the sub categories, by returning the category_loop variable as an array. If you needed to count subcategories in other forms, you'd have to use something similar to what you are trying to do, but there _are_ shorter ways using MySQL queries to count for you.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] count number of sub_categories In reply to
Hmmmm...

I know how this " $_[0]->{'category_loop'} " works...

...what I did not know is what category_loop returns. The hash reference of each link?

It has got to be run an extra time because my category count shows one more that what it actually is...

http://www.magicdirectory.com/...on%2Findex.shtml;d=1
(hopefully I won't have updated it by the time you look so you can see it is one off)

If I subtract one (like when I built earlier - http://www.magicdirectory.com/Magic_Discussion/)... then the count is correct.

I understand arrays, but my mind set is always one off when I think about them. Usually it is careless mistakes.


Thanks dog,

If you figure out why it's one off in this case let me know.

- Jonathan
Quote Reply
Re: [jdgamble] count number of sub_categories In reply to
Hi,

<%category_loop%> is an array, holding a list of hashrefs. Example;

Code:
sub {

my @loop;
my $sth = $DB->table('Links')->select( GT::SQL::Condition->new('Title','LIKE','A%') ) || die $GT::SQL::error;
while (my $hit = $sth->fetchrow_hashref) {
push @loop, $hit;
}

return { random_loop_a => \@loop };
}

..and then in your template, you can use;

Code:
<%if random_loop_a%>
<%loop random_loop_a%>
<%include link.html%><BR>
<%endloop%>
<%endif%>

The way LSQL handles loops, is to return them with \@array, which can then be accessed with the "loop" function. Anyway... you probably know most of this already.. so I'll finish now =)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [jdgamble] count number of sub_categories In reply to
Hi,

I've run this on two sites, and don't get an error. I have the correct number of categories showing on each one, no matter how far down the directories I run.

I looked at your site, but I can't see where you are returning the number of categories.

The page you sent me to, if you put <%get_num_cats%> would show (should show) "5" and the five sub cats you have listed.

How this works if some of the categories are "related" I'm not sure, unless they are returned as part of the <%category_loop%> variable they would not get counted.

category_loop is an array of hash_refs. Each hash_ref points to a data structure of the category record. The hash_refs are put into an array, so that the sort order is preserved. By using category_loop, you can do fancy html output in the templates (same for link_loop). But, the flip side, is you can also find some things out about the array, such as it's size.

There is trivial overhead for this, since Links returns the category_loop variable by default. It creates that datastructure at the same time it's generating the <%links%> output in flat html. So again, the processing overhead is trivial, at the expense of a bit of memory.

If you are getting a different result, I'd like to see it, maybe something else is going on.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] count number of sub_categories In reply to
Well (after a week of not replying),

After I read these replys I had changed it back to the original code. I'm not sure why I had to subtract one at first, but I don't any more.

I have changed so many things then that I am sure I had a reason....?

Anyways,

Thanks for the replys,

- Jonathan
Quote Reply
Re: [jdgamble] count number of sub_categories In reply to
Just in case if anyone needs it, here is a global that I am using to count number of subcategories in any given category.

sub {
# GLOBAL NAME: Count_Subcategories
# PURPOSE: Count the number of sub-categories within a given category.
# CALL TAG: <%Count_Subcategories($ID)%>

my $category_id = shift; # Get the category ID passed to the global

# Ensure the category ID is provided
return 0 unless $category_id;

# Access the Category table
my $category_table = $DB->table('Category');

# Count sub-categories
my $subcategory_count = $category_table->count({ FatherID => $category_id });

return $subcategory_count; # Return just the count
}

Vishal
-------------------------------------------------------
Quote Reply
Re: [VishalT] count number of sub_categories In reply to
Not that your one wouldn't work - but a trimmed down version :)

Code:
sub {
# GLOBAL NAME: Count_Subcategories
# PURPOSE: Count the number of sub-categories within a given category.
# CALL TAG: <%Count_Subcategories($ID)%>

# Ensure the category ID is provided
return 0 unless $_[0] =~ /^\d+$/;

# Count sub-categories
return $DB->table('Category')->count({ FatherID => $_[0] });

}

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] count number of sub_categories In reply to
Andy,

As always think you for the help and guidance.. really appreciate it :)

Vishal

Vishal
-------------------------------------------------------