in reply to Logic problem on loops

Why is there nothing in your second query ("SELECT DISTINCT " .$fa...) to specify which month you're working on? Do you get the exact same too-big numbers for all trimesters? I would expect this to happen, because it looks like you are doing

while (@rown = $sthn->fetchrow_array())

over the same set of rows in each of the twelve iterations through the months.

So, does the DB schema provide any information to identify which month applies to each of the various rows containing "first" and "second" "predicted" and "realized" values? If so, work that into the query. If not, you can't do the trimester tabulation at all, no matter how good your Perl code is.

If/when you solve that problem, then think of the tabulation this way (it will make the code shorter and easier):

foreach $filiais (@filiais){ # print an html header (you don't need $cnt2 for this) $tf = $trf = $tm = $trm = 0; # set accumulators to 0 foreach $month (1 .. 12) { # query DB for rows of 4 values _for_this_month_ while (@rown = $sthn->fetchrow_array()) { ($fa,$rfa,$ma,$rma) = @rown; $tf += $fa; # and likewise for the other three accumulators } if ( $month % 3 == 0 ) { # true for months 3,6,9,12 ListTrimestre( $tf,$trf,$tm,$trm,$month/3 ); $tf = $trf = $tm = $trm = 0; # reset accumulators to 0 } } }

Replies are listed 'Best First'.
Re: Re: Logic problem on loops
by graff (Chancellor) on Apr 21, 2002 at 21:56 UTC
    OOOPS! I'm sorry -- I missed how you were working the month number into the query. But "dws" is right about the structure of the table; I was assuming that "month" information would naturally be in a column of its own.

    In any case, I'd still recommend trying the approach in my ealier reply -- a single block that does the same thing for every month, with a little sub-block that does the special thing at the end of each trimester.

    BTW, is there any chance that data from multiple years are getting mixed into your tables, or are you certain that the table starts out empty every January?