Gossamer Forum
Home : Products : DBMan : Customization :

"Total amount = ???" #Add all amounts and print itas a search result ?

Quote Reply
"Total amount = ???" #Add all amounts and print itas a search result ?
Hi,
Please help me with this modification.

Let's think there are following fields.
1)date
2)name
3)amount
4)somthing_else....more...


When I input two search options like a specific date and a name, can I get the total amount printed as a result like following ?

# seach result
#############################################
Date = yy/mm/dd
---------------------------------------------
data_1. name - amount - something else
data_2. name - amount - something else
.....and more....
=============================================
"Total data numbers = ???" on the input date.
"Total amount = ???" # Add all amounts of each data and print the toal amount here.

Thank you in advance


[This message has been edited by koreags (edited July 11, 1999).]
Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
It really helps if you tell me what you need when you first ask for it. I can only base my response on what you ask for.

Quote:
Only one problem is when the data number is 1, the amount print "0", not real amount.

Would you please give the solution to print out real value ?
I don't understand.

Are you wanting to group the results based on the "name" field, instead of printing out all the records with the name that was entered in a search?


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





Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
The total number of records that fit the search criteria is easy. Just use

$db_total_hits

The total of the "amount" variable is a little tougher.

You could probably add a line to sub html_record, after

my (%rec) = @_;

that would be

Code:
$total_amount += $rec{'amount'};

You would put the "totals" lines in sub html_view_success, after

Code:
for (0 .. $numhits - 1) {
print "<P>";
&html_record (&array_to_hash($_, @hits));
}


I think the following would give you what you want:

Code:
print qq|
===========================================<BR>
Total data numbers = $db_total_hits<BR>
Total amount = $total_amount
|;

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





Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
Thank you JPD.

It is working good.

Only one problem is
when the data number is 1, the amount print "0", not real amount.

Would you please give the solution to print out real value ?

And I gave you very poor explaination at above. so, your solution is little away from what I want.

# Add all amounts of same name and print the amount.

"name" field is select option.
Any data which has name1 at name field add all amount and print it.
name2, name3 .... should be same like bellow.
# seach result
---------------------------------------------
name1 - total name1 amount = ???
name2 - total name2 amount = ???
name3 - total name3 amount = ???
.....and more....
=============================================
"Total amount = ???

From your solution, I can get only "Total amount = ???.

You may have this solution.
It will be very appreciated if you help me.

Thank you in advance


[This message has been edited by koreags (edited July 11, 1999).]
Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
JPD

If there becomes one search result,
the total amount is printed always "zero".
Another word, there must be more than one data. So, it prints correct total amount.


There should be printed out all the records as a search result first.
And then amount.

Just same way you directed me in fomer.
The difference is print something more with the total amount.
Let's say there are "a", "b", "c" and "d" selection option on name field.
First, Add all amount of "a" at first line, if matchs with "a" in name field.
"b", "c" and "d" should be same.
=============================================
Finally print Total amount.

It will look as following;
1. Print all of the search result with all of the records.
2. Print total amount "a", "b", "c" and "d"
Total amount of "a" = 30
Total amount of "b" = 10
Total amount of "c" = 9
Total amount of "d" = 20
=============================================
3. Print Total amount of a+b+c+d = 69

I hope I explained enough.

[This message has been edited by koreags (edited July 11, 1999).]
Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
Are you using the "short" display mod?

Code:
for (0 .. $numhits - 1) {
print "<P>";
&html_record (&array_to_hash($_, @hits));
}

add

Code:
# change the values below to match the field numbers of your name and amount fields
$name_field = 2;
$amount_field = 3;

for (0 .. $numhits - 1) {
$name = $hits[$_ * ($#db_cols+1) + $name_field];
$amount = $hits[$_ * ($#db_cols+1) + $amount_field];
$total{$name} += $amount;
$grand_total += $amount;
}
foreach $key (sort keys %total) {
print qq|
Total amount of "$key" = $total{$key}<BR>
|;
}
print qq| ===========================================<BR>Total amount = $grand_total
|;



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





Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
JPD.

It is working good.
Thank you for this.

There is printed very long data because I have lots of selection options in name field.
So I guess I need new page with clicking a link from search result page.

I think I should make new sub-routine for this. Then, print link name at search result page.

Is it possible ?


I must use "short" display mod as soon as you checked your short display mod.

Is it cause any diffrences ?


[This message has been edited by koreags (edited July 11, 1999).]
Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
I'll have to get to this later. I'm not sure how to do your subroutine, since it will have to duplicate the search. I'll have to think about it a bit.

I'll look into the "short" mod and see if I can find the problem. Look for a response on your other thread.


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





Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
JPD

This works fine with short dispaly mod too.

I have another question.

Can be printed the total data numbers of "a", "b", "c" and "d" also ?
Toal numbers = 9 | Total amount of "a" = 30
Toal numbers = 4 | Total amount of "b" = 10
Toal numbers = 3 | Total amount of "c" = 9
Toal numbers = 7 | Total amount of "d" = 20

Thank you.
Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
Thank you JPD
Your mod Works good.

I tried to print name field and another field in one page. but got error.

Would you please direct me with this ?

I tried two ways
First,=======================================
$name_field = 2;
$another_field = 8;
$amount_field = 3;
for (0 .. $numhits - 1) {
$name = $hits[$_ * ($#db_cols+1) + 2];
$another = $hits[$_ * ($#db_cols+1) + 8];
$amount = $hits[$_ * ($#db_cols+1) + 3];
$total{$name} += $amount;
$total{$another} += $amount;
$grand_total += $amount;
}
foreach $key (sort keys %total) {
print qq|
Total amount of "$key" = $total{$key}<BR>
|;
}
This prints right data and value.
But the sort was mixed up like bellow.
name1
another1
name2
name3
another2

Second,======================================
I also tried it two times separately as following;
# A. for name field.
for (0 .. $numhits - 1) {
$name = $hits[$_ * ($#db_cols+1) + 2];
$amount = $hits[$_ * ($#db_cols+1) + 3];
..
}
foreach $key (sort keys %total) {
#++++++++++++++++++++++++++++++++++++++++++++
# B. for another field.
for (0 .. $numhits - 1) {
$another = $hits[$_ * ($#db_cols+1) + 8];
$amount = $hits[$_ * ($#db_cols+1) + 3];
..
}
foreach $key (sort keys %total) {

From this trying,
the name field is printed correctly.
but,
at the another field,
there are printed name field and another field with mixed sort.

Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
You can't do it like that, if you want to keep things together.

What do you want?


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





Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
Yes.

Code:
# change the values below to match the field numbers of your name and amount fields
$name_field = 2;
$amount_field = 3;
for (0 .. $numhits - 1) {
$name = $hits[$_ * ($#db_cols+1) + $name_field];
$amount = $hits[$_ * ($#db_cols+1) + $amount_field];
$total{$name} += $amount;
++$count{$name};
$grand_total += $amount;
++$total_count;
}
foreach $key (sort keys %total) {
print qq~
Total numbers = $count{$key} |
Total amount of "$key" = $total{$key}<BR>
~;
}
print qq| ===========================================
<BR>Total numbers = $total_count |
Total amount = $grand_total
|;

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





Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
Let's think there is "name" field, and has select option "a", "b", "c" and "d".
Then print following;
<table bgcolor=red>
numbers = 9 | Total amount of name "a" = 30
numbers = 4 | Total amount of name "b" = 10
numbers = 3 | Total amount of name "c" = 9
numbers = 7 | Total amount of name "d" = 20
</table>
And, guess there is also "person" field has select option 1,2 and 3.
Then print also the following in same page, but use different table.
<table bgcolor=green>
numbers = 8 | Total amount of person "1" = 50
numbers = 5 | Total amount of person "2" = 30
numbers = 2 | Total amount of person "3" = 60
</table>

Both of name and person field concerned same amount field.

[This message has been edited by koreags (edited July 12, 1999).]
Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
You can't use the

$total{$another}

variable. You need to have another name for the hash.

Use

$total2{$another}

If you know that none of your $another values will be the same as your $name values, you can still use ++$count{$another}, though, as long as you follow the pattern I gave you before. If there's a chance that even one of your $name values will be equal to any of your $another values, you'll need to use

++$count2{$another}

instead.

Does this make any sense?


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





Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
Thank you

It works but little problem.

Let's think
The name field can't be blank, but person field "can be left blank".
There are total of 15 records.
so, total number of name field will be 15.
Total number of person field can be less than the total number of name field.

The name field is printed correctly.
name-a => num=5 amount=300
name-b => num=4 amount=200
name-c => num=4 amount=400
name-d => num=2 amount=500
---------------------------
total num=15 amount=1,400

The person field should be printed as following;
person-1 => num=4 amount=200
person-2 => num=3 amount=300
person-3 => num=1 amount=100
---------------------------
total num=8 amount=600

However, the person field printed as following;
=> num=7 amount=700 # with blank optoin name
person-1 => num=4 amount=200
person-2 => num=3 amount=300
person-3 => num=1 amount=100
---------------------------
total num=15 amount=1,400

How can I solve this problem.
Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
Code:
# change the values below to match the field numbers of your name, amount and person fields
$name_field = 2;
$amount_field = 3;
$person_field = 4;
for (0 .. $numhits - 1) {
$name = $hits[$_ * ($#db_cols+1) + $name_field];
$amount = $hits[$_ * ($#db_cols+1) + $amount_field];
$person = $hits[$_ * ($#db_cols+1) + $person_field];
$total{$name} += $amount;
++$count{$name};
$grand_total += $amount;
++$total_count;
if ($person) {
$person_total{$person} += $amount;
++$person_count{$person};
$grand_total_person += $amount;
++$total_person_count;
}
}
print "<table bgcolor=red>";
foreach $key (sort keys %total) {
print qq~<TR><TD>
Total numbers = $count{$key} </TD><TD>
Total amount of "$key" = $total{$key}</TD></tr>
~;
}
print qq| <TR><TD colspan=2>
===========================================
<TR><TD>Total numbers = $total_count </TD><TD>
Total amount = $grand_total
</td></tr></table>|;

print "<table bgcolor=green>";
foreach $key (sort keys %person_total) {
print qq~<TR><TD>
Total numbers = $person_count{$key} </TD><TD>
Total amount of "$key" = $person_total{$key}</TD></tr>
~;
}
print qq| <TR><TD colspan=2>
===========================================
<TR><TD>Total numbers = $total_person_count </TD><TD>
Total amount = $grand_total_person
</td></tr></table>|;

Are there any more changes you want to this?

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





Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
Yes, Sir.

Your mod works good. Thank you.

However, there is a problem.
It only counts displayed records, not total.
When I select 10 records per page to search, it counts only for 10 records in that page, and last pages are also display only for 10 records.


How about this.

1. Give two links option to search "records" or "analysis".
2. Normal search page: When I search something from normal search page, It only dispalys records.
3. Analysis search page: it dispays only analysis, not records.
In this case, the records should be hidden.
4. Both of above should be started from the page of "html_record_form".
5. It will be best if there is pulldown select menu to dispaly something only user wants to check.
For example, if user inputs search words in table and choose "name" field at bottom just like to sort records, only the name field will be displayed.
if choose "person" field, only displays name field analysis.
if choose both of "name" and "person" field, it displays both analysis on one page.

I guess it will take much time from you.
Sorry for this.

Thank you.

[This message has been edited by koreags (edited July 13, 1999).]
Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
Then everything I've given you has been a waste of time.

I don't understand this:
Quote:
4. Both of above should be started from the page of "html_record_form".

Let me see if I understand you correctly.

You want to have two links in your footer menu. One link is the regular "View" link, which takes the user to the search form and performs the search based on the fields the user fills in. The result is the regular html_record display.

The other link, called "Analysis" takes the user to a form like the search form. The user either fills in data he wants to search for or not. He also decides whether he wants the analysis on the "name" field, the "person" field, or both.

The result is the table, which tallies the amount and count of all the records in the database which fit the search criteria (if any) and displays one or the other or both of the tally tables, depending on what the user selected in the field.

Right?

This could be very problematic. How many fields are we talking about that the user could enter search terms for?


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





Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
Wow, you understand my poor English well enough.

html_record_form means "search form" <== forget it.

Yes, two links at footer "view" and "analysis".

Quote:
The user either fills in data he wants to search for or not. He also decides whether he wants the analysis on the "name" field, the "person" field, or both.

Let's guess,
User wants to count name field for year of 1999.
Then, he should first input "1999" at date field on search form.
Then, select "name field" at bottom just like "Search Options: Sort By: Sort Order" of dbman.

In select manu, all fields name should be shown up.
So, he can get the count result of name field, person field or more in year of 1999.
If this select option makes you complecate. just forget it.

Now, let's try to display all tallys in one page as a count result.
If I input 1999 at search form, the analysis result of name and person field or more whould be displayed depanding on more fields I set on html.pl.
Quote:
How many fields are we talking about that the user could enter search terms for?

It can be name, person and more fields.

Thank you.


[This message has been edited by koreags (edited July 13, 1999).]
Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
How many more fields? I need a number.


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





Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
The fields should be "8"

Sorry for late answer.
Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
I'll try once more.

Add lines to sub main (db.cgi):

Code:
elsif ($in{'analyze_search'}) { if ($per_view) { &html_analyze_search; } else { &html_unauth; } }
elsif ($in{'analyze_records'}) { if ($per_view) { &analyze_records; } else { &html_unauth; } }

Add a subroutine to db.cgi:

Code:
sub analyze_records {
# --------------------------------------------------------
my ($status, @hits) = &query("view");
if ($status eq "ok") {
&html_analyze_success(@hits);
}
else {
&html_analyze_failure($status);
}
}


Add a line to html_footer (html.pl):

Code:
print qq!| <A HREF="$db_script_link_url&analyze_search=1">Analysis</A> ! if ($per_view);

Add a subroutine to html.pl:

Code:
sub html_analyze_search {
# --------------------------------------------------------
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;

$mh=scalar(@lines);

&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: Analyze the Database.</title>
</head>

<body bgcolor="#DDDDDD">
<form action="$db_script_url" method="GET">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<input type=hidden name="mh" value="$mh">
<center>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 align=center valign=top>
<tr><td colspan=2 bgcolor="navy">
<FONT FACE="MS Sans Serif, arial,helvetica" size=1 COLOR="#FFFFFF">
<b>$html_title: Analyze the Database</b>
</td></tr>
<tr><td>
<p><center><$font_title><b>
Analyze the Database
</b></font></center><br>
<$font>|;
&html_record_form();
print qq|
<select name="analysis">
<option>Name
<option>Person
<option>Both
</select>|;
print qq|</font></p>
<p><center> <INPUT TYPE="SUBMIT" NAME="analyze_records" VALUE="Analyze Records">
<INPUT TYPE="RESET" VALUE="Reset Form"></center></p>
|;
&html_footer;
print qq|
</td></tr>
</table>
</center>
</form>
</body>
</html>
|;
}

Add a subroutine to html.pl:

Code:
sub html_analyze_success {
# --------------------------------------------------------

my (@hits) = @_;
my ($numhits) = ($#hits+1) / ($#db_cols+1);

# change the values below to match the field numbers of your name, amount and person fields
$name_field = 2;
$amount_field = 3;
$person_field = 4;
for (0 .. $numhits - 1) {
$name = $hits[$_ * ($#db_cols+1) + $name_field];
$amount = $hits[$_ * ($#db_cols+1) + $amount_field];
$person = $hits[$_ * ($#db_cols+1) + $person_field];
$total{$name} += $amount;
++$count{$name};
$grand_total += $amount;
++$total_count;
if ($person) {
$person_total{$person} += $amount;
++$person_count{$person};
$grand_total_person += $amount;
++$total_person_count;
}
}

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

<body bgcolor="#DDDDDD">
<blockquote>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 valign=top>
<tr><td colspan=2 bgcolor="navy"><FONT FACE="MS Sans Serif, arial,helvetica" size=1 COLOR="#FFFFFF">
<b>$html_title: Analysis Results</b>
</font></td></tr>
</table>
<p><$font>
|;
if (($in{'analysis'} eq "Name") or ($in{'analysis'} eq "Both")) {
print "<table bgcolor=red>";
foreach $key (sort keys %total) {
print qq~<TR><TD>
Total numbers = $count{$key} </TD><TD>
Total amount of "$key" = $total{$key}</TD></tr>
~;
}
print qq| <TR><TD colspan=2>
===========================================
</TD></TR>
<TR><TD>Total numbers = $total_count </TD>
<TD>Total amount = $grand_total
</td></tr></table>|;
}
if (($in{'analysis'} eq "Person") or ($in{'analysis'} eq "Both")) {
print "<table bgcolor=green>";
foreach $key (sort keys %person_total) {
print qq~<TR><TD>
Total numbers = $person_count{$key} </TD>
<TD>Total amount of "$key" = $person_total{$key}</TD></tr>
~;
}
print qq| <TR><TD colspan=2>
===========================================</TD></TR>
<TR><TD>Total numbers = $total_person_count </TD>
<TD>Total amount = $grand_total_person</td></tr></table>|;
}
print qq|
<p>
<table border=0 bgcolor="#DDDDDD" cellpadding=5 cellspacing=3 width=500 valign=top>
<tr><td>|;
&html_footer;
print qq|</td></tr>
</table>
</blockquote>
</body>
</html>
|;
}

Add subroutine to html.pl:

Code:
sub html_analyze_failure {
# --------------------------------------------------------

my ($message) = $_[0];

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;

$mh=scalar(@lines);

&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: Analysis Failed</title>
</head>

<body bgcolor="#DDDDDD">
<form action="$db_script_url" method="GET">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<input type=hidden name="mh" value="$mh">
<center>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 align=center valign=top>
<tr><td colspan=2 bgcolor="navy">
<FONT FACE="MS Sans Serif, arial,helvetica" size=1 COLOR="#FFFFFF">
<b>$html_title: Analysis Failed</b>
</td></tr>
<tr><td>
<p><center><$font_title><b>
Analysis Failed
</b></font></center><br>
<$font><$font>
<P>There were problems with the analysis. Reason: <FONT COLOR="red"><B>$message</B></FONT>
<BR>Please fix any errors and submit the record again.</p>|;
&html_record_form();
print qq|
<select name="analysis">
<option>Name
<option>Person
<option>Both
</select>|;
print qq|</font></p>
<p><center> <INPUT TYPE="SUBMIT" NAME="analyze_records" VALUE="Analyze Records">
<INPUT TYPE="RESET" VALUE="Reset Form"></center></p>
|;
&html_footer;
print qq|
</td></tr>
</table>
</center>
</form>
</body>
</html>
|;
}


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





Quote Reply
Re: "Total amount = ???" #Add all amounts and print itas a search result ? In reply to
Thanks JPD

It works fine.

It is most usefull function for me.

Thank you again.