The "ListaTrimestre" sub is a simple "put the vars into HTML code", so it displays later in the template. It is the $tft, $trft, $tmt and $trmt vars that are wrong. The problem is that I didn't found a pattern of error, so I acn't find where is the value that is being incorrectly added to the totals.sub acumulado_f #I've called a sub 'cause there's more than one type o +f report { $titulo = "Report by state"; $dbh = SysUtils::Connect; $sthf = $dbh->prepare_cached("SELECT uf FROM filiais ORDER BY uf") +; #selects each state $xf = 0; $sthf->execute; while (@dataf = $sthf->fetchrow_array()) { $filiais[$xf] = $dataf[0]; $xf++; } foreach $filiais(@filiais)#foreach state { $cnt =0; $cnt2 = 0; if ($cnt2 eq 0)# if it's the first result, assemble a header { $message .= "<BR><TABLE WIDTH=\"100%\" BORDER=1> <TR class=\"texto3\"> <TD colspan=\"9\" BGCOLOR=\"#FFFFFF\"><span class =\"texto +2\">State: $filiais</span></TD> </TR> <TR> <TD WIDTH=\"10%\"> </TD> <TD COLSPAN=\"4\" class=\"texto2\" align=\"center\">Incom +e</TD> <TD COLSPAN=\"4\" class=\"texto2\" align=\"center\">Margi +n</TD> </TR> <TR> <TD WIDTH=\"10%\" class=\"texto2\"> </TD> <TD WIDTH=\"13%\" ALIGN=\"CENTER\" class=\"texto2\">Predic +ted</TD> <TD WIDTH=\"13%\" ALIGN=\"CENTER\" class=\"texto2\">Realiz +ed</TD> <TD WIDTH=\"5%\" ALIGN=\"CENTER\" class=\"texto2\">%</TD> <TD WIDTH=\"13%\" ALIGN=\"CENTER\" class=\"texto2\">Differ +ence</TD> <TD WIDTH=\"13%\" ALIGN=\"CENTER\" class=\"texto2\">Predic +ted</TD> <TD WIDTH=\"13%\" ALIGN=\"CENTER\" class=\"texto2\">Realiz +ed</TD> <TD WIDTH=\"5%\" ALIGN=\"CENTER\" class=\"texto2\">%</TD> <TD WIDTH=\"14%\" ALIGN=\"CENTER\" class=\"texto2\">Differ +ence</TD> </TR>"; $cnt2++; } for ($m=1; $m<=12; $m++)#Make an array of months { $months[$m] = $m; } foreach $months(@months) { if ($months ne "")#escapes the obligatory 0 position of th +e array - blame me later for this one :) { $sh_mes = SysUtils::Extenso_Mes("$months");#Return the + month name $fat = 0; $mar = 0; # In the database there are fields with the same number as months: $fa = "A.f".$months; $ma = "A.m".$months; $rf = "A.rf".$months; $rm = "A.rm".$months; $tf = 0; # First predicted value $trf = 0;# First realized value $tm = 0;# Second Predicted value $trm = 0;# Second realized value $ctp = 0; $sthn = $dbh->prepare_cached("SELECT DISTINCT ".$fa." +, ".$ma." , ".$rf." , ".$rm." FROM metas A, usuarios B WHERE B.filial +='$filiais' AND A.login=B.login") or die "Preparing error. ".$dbh->er +rstr; $sthn->execute or die "executing error. ".$sthn->errst +r; #This statement works fine. I've debugged it and tested it while (@rown = $sthn->fetchrow_array()) { $fat = $rown[0]; $mar = $rown[1]; $rfat = $rown[2]; $rmar = $rown[3]; $tf += $fat; #First predicted value $tm += $mar; #Second predicted value $trf += $rfat;#First realized value $trm += $rmar;#Second realized value $ctp++; if ($months eq "1") #January, obviously { $tft = 0; #trimester predicted 1st total $trft = 0; #trimester realized 1st total $tmt = 0; #trimester predicted 2nd total $trmt = 0; #trimester realized 2nd total $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "2" or $months eq "3") { $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "4") { $tft = 0; $trft = 0; $tmt = 0; $trmt = 0; $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "5" or $months eq "6") { $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "7") { $tft = 0; $trft = 0; $tmt = 0; $trmt = 0; $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "8" or $months eq "9") { $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "10") { $tft = 0; $trft = 0; $tmt = 0; $trmt = 0; $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } elsif ($months eq "11" or $months eq "12") { $tft += $tf; $trft += $trf; $tmt += $tm; $trmt += $trm; } } $sthn->finish; if ($ctp ne 0) { ListaDados(); #brings value for each month if ($months eq "3")# Is it a trimester? { $trim = 1; ListaTrimestre(); #bring values (totals) for e +ach trimester } elsif ($months eq "6") { $trim = 2; ListaTrimestre(); } elsif ($months eq "9") { $trim = 3; ListaTrimestre(); } elsif ($months eq "12") { $trim = 4; ListaTrimestre(); } } else { if ($cnt eq 0)#No values to show { $message .= "<TR CLASS=\"texto3\"> <TD BGCOLOR=\"$color2\" colspan=\"9\">State ha +s no values.</TD> </TR>"; } $cnt++; } } } } $dbh->disconnect; #open template to show values. print "Content-type: text/html\r\n\r\n"; open (FH,"templates/relatorios/relatorio_andamento.htm") or die "C +ant open body: $!"; $body = join('',<FH>); close (FH); eval "\$body = sprintf \"%s\",<<FYNYS;\n $body\nFYNYS\n"; print $body; }
In reply to Logic problem on loops by DaWolf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |