Gossamer Forum
Home : Products : DBMan : Customization :

Calculating

(Page 2 of 2)
> >
Quote Reply
Re: Calculating In reply to
Hey, I almost forgot the other part that I needed!

I need to display to only the owner of the record the following info when they call for it:
item1 = ______
item2 = ______
item3 = ______
total = ______

In this case each record will not be listed, only the total value of each field value.

Essentially there are only 3 possible values this field can have (item1, item2, item3). So if field=item1 then add them all up, but don't display each record only the total. And the same for item2 and item3.

I want this accessible via a simple link (click here for item totals).

Again, consider without the relational db, just the original html.pl & db.cgi

Adam

Quote Reply
Re: Calculating In reply to
I don't know how to do what you want. I don't know what code you are using, so I don't know how to alter it. Sorry.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Calculating In reply to
I thought about this a little and maybe I can give you a pointer or two.

Create a new subroutine to display the totals. You would need to be in your "items" database.

At the beginning of the subroutine, do the search and collect the totals:

Code:

$in{$db_cols[$auth_user_field]} = $db_userid;
my ($status,@hits) = &query("view");
my ($numhits) = ($#hits+1) / ($#db_cols+1);
my ($maxhits); $in{'mh'} ? ($maxhits = $in{'mh'}) : ($maxhits = $db_max_hits);
for (0 .. $numhits - 1) {
my (%rec) = &array_to_hash($_, @hits);
$total{$rec{Field}} += $rec{OtherField};
$grand_total += $rec{OtherField};
}
When you want to print them out, use

Code:

foreach $item (sort keys %total) {
print qq|$item = $total{$item}|;
}
print qq|Total = $grand_total|;
You'll also need to add a line in db.cgi, sub main to point to this subroutine when they click the link.

I have no idea how this would work with templates.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Calculating In reply to
I have updated this post to reflect the problems I have fixed from my original post of this code and to reflect where I am now -- Which appears to be WORKING!! Smile

Here are the change I made:

In db.cgi

I added in:
sub main
elsif ($in{'view_totals'}) { if ($per_view) { &view_html_totals; } else { &html_unauth; } }

Then I added also in db.cgi
sub view_totals {
# --------------------------------------------------------
$in{$db_cols[$auth_user_field]} = $db_userid;
$in{'mh'} = 100000; # That should be enough to cover all possible records
my ($status,@hits) = &query("view");
my ($numhits) = ($#hits+1) / ($#db_cols+1);
# my ($maxhits); $in{'mh'} ? ($maxhits = $in{'mh'}) : ($maxhits = $db_max_hits); #don't need this then
for (0 .. $numhits - 1) {
my (%rec) = &array_to_hash($_, @hits);
$total{$rec{'Field1'}} += $rec{'Value1'};
$grand_total += $rec{'Value1'};
}
}

Then in html.pl
I added

sub view_html_totals {
# --------------------------------------------------------

&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: View Totals.</title>
</head>

<body bgcolor="#DDDDDD">
<center>
|;

&view_totals;
foreach $Field1 (sort keys %total) {
print qq|$Field1 = $total{$Field1}|;
}
print qq|Total = $grand_total|;
print qq|
</center>
</body>
</html>
|;
}



Thanks again!
Adam

Quote Reply
Re: Calculating In reply to
Looks good! Smile

JPD
http://www.jpdeni.com/dbman/
> >