You want to count the total number of records and the number of records that have the same value in a field. Is that correct?
Yes, that's been discussed quite a bit. But I'll go over it again.
First, you need to get the field number you want to count. If, for example, you wanted to know how many records were in each different category in the "Category" field, you would use the number of the "Category" field. For my purposes, I'm going to use the number 6, because I like that number.
I'll put it in bold print so you'll be able to find where to change it.
Most people print counts on their html_home page, to give users an idea of what's in the database. If that's what you want to do, this code will go into html_home, right at the beginning. If not, put it at the beginning of whatever subroutine matches the page you want to print it out on.
Code:
open (DB, "<$db_file_name") or &cgierr("unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
foreach $line(@lines) {
if ($line =~ /^$/) { next; }
if ($line =~ /^#/) { next; }
@data = split /$db_delim/, $line;
++$count{$data[
6];
++$total_count;
}
When you want to print out your data, you can use something like:
Code:
print "There are $total_count records in the database.<BR>"
foreach $key (sort keys $count) {
print "$key: $count{$key}<BR>";
}
------------------
JPD
[This message has been edited by JPDeni (edited July 08, 1999).]