Gossamer Forum
Home : Products : DBMan : Customization :

Grouped Totals

Quote Reply
Grouped Totals
Just wondering if the following would be possible.

Is it possible to generate an html page where the amount of records (e.g. divesites in a particular country) counted for each country is listed ?

The page would be a combination of all countries that have one or more records and the total for each country ?

Any help would greatly be appreciated but I'm not quite sure it will be possible with DBMan.

Thanks anyway

------------------
Michael De Coninck
C-Ware IT Consultants
http://www.c-ware.net
Quote Reply
Re: Grouped Totals In reply to
Do you want this to be a count of all the records in the database, or only those that are returned from a search?


------------------
JPD





Quote Reply
Re: Grouped Totals In reply to
Thanks for the fast response :-)

What I would like is e.g.: the database has 2 records for Belgium, 3 records for France and no records for Luxembourg. From this I would like to automatically generate a list that looks like the following

Belgium (2)
France (3)
Luxembourg (0) or locations with 0 records can be omitted.

Quote Reply
Re: Grouped Totals In reply to
So you want the full database. Smile It really does make a difference.

The easiest way to do it is to not have the ones with 0 records printed. If you need it later, I think I can figure out what is necessary to do that. (I've done it before. Smile )

I'm assuming you want this on the first page after users log in -- the database home page.

If so, in sub html_home (html.pl file) add the following right at the beginning of the script:

Code:
open (DB, "<$db_file_name") or &cgierr("error in count.
unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;

foreach $line (@lines) {
chomp ($line);
@data = &split_decode($line);
++$count{$data[10]};
++total_count;
}

In the line

++$count{$data[10]};

substitute the 10 for the number of the field that has the country value.

In the place where you want to print out the countries and totals use:

Code:
print qq|Country list:<P><table>|;
foreach $country (%count) {
print qq|<tr><td>$country:</td><td>$count{$country}</td></tr>|;
}
print qq|<tr><td>Total:</td><td>$total_count</td></tr></table>|;
}

I think that should do it. You'll probably want to play around with the formatting of the table so it looks nice.


------------------
JPD





Quote Reply
Re: Grouped Totals In reply to
How to solve this minor glitch...

Code:
print qq|Country list:<P><table>|;
foreach $country (%count) {
print qq|<tr><td>$country:</td><td>$count{$country}</td></tr>|;
}
print qq|<tr><td>Total:</td><td>$total_count</td></tr></table>|;
}


In the abovementioned foreach statement the whole hash is being processed ... e.g. Austria, 1, Belgium, 3 .... which gives funny results in the lay out. It would be better to have the foreach only go through the country names and skip the totals ... because now you get a result like:

Austria: 1
1:
Belgium: 3
3:

but how to do this ?

TIA


------------------
Michael De Coninck
C-Ware IT Consultants
http://www.c-ware.net

[This message has been edited by MichaelDC (edited August 04, 1999).]
Quote Reply
Re: Grouped Totals In reply to
Really? I don't see how you would get

Quote:
Austria: 1
1:
Belgium: 3
3:

Let me look at the code again.

I don't see how this is possible.

Quote:
It would be better to have the foreach only go through the country names and skip the totals

That's what it does. The subscripts in the %count hash are the country names.

Can I see this in action?

------------------
JPD





Quote Reply
Re: Grouped Totals In reply to
I've found a work around using an IF clause ... I'll put up the old code and let you know when it is up there so you can take a look at it.

Greetz and TIA

Mike