Gossamer Forum
Quote Reply
Count
I have the list category & give count mod up and working fine. However, no where in the mod does it allow for a "total count" after the list is made.

I spent the last couple of days reading about counts & experimenting. The most I could actually get it to do is print:

Total Count =

Here is the mod (notice the blue below):

------------------------------

The mod already lists the individual counts of each category (in my case it is called "symbol".

for ($i = 0; $i <= $#db_cols; $i++) {
#### In the line below, replace 'Category' with the name of your field.
if ($db_cols[$i] eq "Symbol" ) {
$fieldnum = $i; $found = 1;
last;
}
}
if ($found) {
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
++$count{$fields[$fieldnum]};
}
close DB;
#### In the line below, replace 'Category' with the name of your field.
@options = split (/\,/, $db_select_fields{'Symbol'});

foreach $option (sort @options) {
if ($count{$option}) {
$encoded = &urlencode($option);
#### In the line below, replace 'Category' with the name of your field.
print qq|
<TABLE WIDTH="20%" CELLPADDING=0 CELLSPACING=0 BORDER=1 BGCOLOR="#EDDCDC">
<tr> <td width="30%" align="left"><b><Font face="Verdana, Arial, Helvetica" Size="2"><a href="$db_script_link_url&Symbol=$encoded&view_records=1">$option</FONT></b></TD>
<td width="10%" align="center"><font face="Verdana, Arial, Helvetica" size="2"><b>$count{$option}</b></font></TD>
</TR>
</TABLE>
|;
}
}
}
# < -- End page text -->

&html_footer;
&html_page_bottom;
}

------------

Should I add code below that, such as:

++$total_count;

below the:

++$count{$fields[$fieldnum]};

and then add something like:

print|

There are $total_count symbols in the database.
;
}


Or, am I totally off base here? Thanks very much for your help.




Diana Rae
Quote Reply
Re: [dianarae] Count In reply to
Yes, I think adding ++$total_count right below or above the blue line would work.

You should also add the line "my $total_count;" at the beginning of the subroutine that this code is in. You $total_count only in this subroutine, so it should be declared locally (through "my").
kellner
Quote Reply
Re: [kellner] Count In reply to
Whoo Hooo thank you Kellner! You ARE the LADY! Cool

And yes it worked. I did what you said & added:

my ($total_count);

at the very beginning of the list category & give count sub - I had to add the parenthesis-.

Then I added the total_count after the fieldnum count:

++$count{$fields[$fieldnum]};
++$total_count;

Then at the very bottom of the Routine (and this is after the individual totals are listed - otherwise this prints on each and every line), so I added it like this -

P.S. My list category & give count page is in columns:

#### In the line below, replace 'Category' with the name of your field.
print qq|
<TABLE WIDTH="20%" CELLPADDING=0 CELLSPACING=0 BORDER=1 BGCOLOR="#EDDCDC">
<tr> <td width="30%" align="left"><b><Font face="Verdana, Arial, Helvetica" Size="2"><a href="$db_script_link_url&Symbol=$encoded&view_records=1">$option</FONT></b></TD>
<td width="10%" align="center"><font face="Verdana, Arial, Helvetica" size="2"><b>$count{$option}</b></font></TD>
</TR>
</TABLE>
|;
}
}
}
# < -- End page text -->
# < -- Start page text -->

print qq|
<br>
<p><font face="Arial" size="2">There are<b> <font color="#880000">$total_count </font></b>Symbols
in the Duchy.</font></p>
|;

# < -- End page text -->
&html_footer;
&html_page_bottom;
}

Thanks again for your help on sorting the end of this out. This is good to know for people that wish to add a Total to the bottom of the List Category & give count mod.

Sly
Diana Rae
Quote Reply
Re: [dianarae] Count In reply to
Don't thank me all too enthusiastically - you *did* write the code all by yourself; I just confirmed that it was ok - glad to know I was right!
kellner