Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grand total loop

Status
Not open for further replies.

vintl

Technical User
Joined
Jun 9, 2000
Messages
72
Location
MY
i got this problem with the grand total in the table.
-------------------------------------------------------
may'01 june'01 july'01 august'01| sub total
file1 1 1 0 0 | 2
file2 2 0 0 0 | 2
file3 3 0 0 0 | 3
-------------------------------------------------------
grandtotal 6 7 7 7 7
-------------------------------------------------------
i guess when u look at this table u know whats is wrong. the grand total for each month is incorrect. i guess its the loop problem, pls help me to figure out where gone wrong in my scripts.

%SUBTOTAL = ();
$grand_total = 0;
$noRec=1;
$stmt = &quot;select count(*) from TB_DOWNLOAD where FILE_ID=? AND DATE_DOWNLOAD>=? and DATE_DOWNLOAD<?&quot;;
$sth = $dbh->prepare($stmt);
foreach $H_FILE_ID (sort { $a <=> $b }keys %ALL_FILE_ID) {
$H_FILE_TITLE = $ALL_FILE_ID{$H_FILE_ID};
print qq(<tr>\n);
print qq( <td align=&quot;center&quot;><font size=&quot;2&quot; face=&quot;Arial&quot;>$noRec</font></td>\n);
print qq( <td><font size=&quot;1&quot; face=&quot;Arial&quot;>$H_FILE_TITLE</font></td>\n);
$sub_total = 0;
for($zz=0;$zz<$F_PERIOD;$zz++) {
($LOOP_START_DATE_YEAR,$LOOP_START_DATE_MONTH,$LOOP_START_DATE_DAY) = Add_Delta_YMD($F_START_DATE_YEAR,$F_START_DATE_MONTH,$F_START_DATE_DAY, 0,$zz,0);
($LOOP_END_DATE_YEAR,$LOOP_END_DATE_MONTH,$LOOP_END_DATE_DAY) = Add_Delta_YMD($LOOP_START_DATE_YEAR,$LOOP_START_DATE_MONTH,$LOOP_START_DATE_DAY, 0,1,0);
$sth->execute($H_FILE_ID,&quot;$LOOP_START_DATE_YEAR-$LOOP_START_DATE_MONTH-$LOOP_START_DATE_DAY&quot;,&quot;$LOOP_END_DATE_YEAR-$LOOP_END_DATE_MONTH-$LOOP_END_DATE_DAY&quot;);
if($row_ref = $sth->fetchrow_arrayref) {
$month_total = $row_ref->[0];
$sub_total += $month_total;
$SUBTOTAL{$zz} += $sub_total;
$month_total = commify($month_total);
print qq( <td width=&quot;$col_width&quot; align=&quot;right&quot;><font size=&quot;1&quot; face=&quot;Arial&quot;>$month_total </font></td>\n);
}
}
$grand_total += $sub_total;
$sub_total = commify($sub_total);
print qq( <td align=&quot;right&quot;><font size=&quot;1&quot; face=&quot;Arial&quot;>$sub_total </font></td>\n);
print qq(</tr>\n);
$noRec++;
}
$sth->finish;


if ($noRec==0) {
print qq(<tr>\n);
print qq( <td colspan=&quot;$table_colspan&quot; align=&quot;center&quot;><font size=&quot;2&quot; face=&quot;Arial&quot;><br>There are no entries to display.</br> </font></td>\n);
print qq(</tr>\n);
}

$grandtotal_colspan = 2 if $noRec != 0;

# print grand total
$grand_total = sprintf(&quot;%d&quot;,$grand_total);
$grand_total_format = commify($grand_total);
print qq(<tr>\n);
print qq( <td bgcolor=&quot;#C0C0C0&quot; colspan=&quot;2&quot;><font size=&quot;2&quot; face=&quot;Arial&quot;>Grand Total</font></td>\n);

@sort_keys = sort {$a <=> $b} (keys %SUBTOTAL);
foreach $ttl (@sort_keys) {
$grandsubtotal_format = commify($SUBTOTAL{$ttl});
$grandsubtotal_format = &quot;$grand_total&quot; if $grandsubtotal_format ne '0';
print qq( <td bgcolor=&quot;#C0C0C0&quot; align=&quot;right&quot;><font size=&quot;1&quot; face=&quot;Arial&quot;>$grandsubtotal_format </font></td>\n);
 
Hi, I can't read peoples code and just correct it very well, but since you wrote it I'm sure you'll know where to apply this,

when you start from point a, and use .+ to add all the values until point b then you must store that value away for safe keeping and undefine it.

undef($total);

Then that way when it goes through the loop again for the next row in your &quot;virtual table of memory&quot; it will begin counting from zero, which would give you the total per row and not carry the previous total through.

Get what I mean?

Tony
 
Instead of undefining it you could also just set it equal to 0.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
ok...u mean
@sort_keys = sort {$a <=> $b} (keys %SUBTOTAL);
foreach $ttl (@sort_keys) {
$grandsubtotal_format = commify($SUBTOTAL{$ttl});
$grandsubtotal_format = &quot;$grand_total&quot; if $grandsubtotal_format ne '0';
print qq( <td bgcolor=&quot;#C0C0C0&quot; align=&quot;right&quot;><font size=&quot;1&quot;
change where $grandsubtotal_format ne '0'; to $grandsubtotal_format eq '0'; ?
or
$grandsubtotal_format ne '0';
undef($grandsubtotal_format); ?
the results either all the Grand total does not show anything and its doesnt solve the problem...
 
no, he means that the first step of calculating the total should be to set the variable that will be that total to zero.
the code you posted is much more than is needed. isolate the part that calculates the totals and post that. i am relcutant, no, we are reluctant to read that much code to fix so small a problem.
regards. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top