Jump to content

codeee troubles!! for loop within a while loop..?


joel24

Recommended Posts

I have been staring at this code for hours and cannot work out what I'm doing wrong, maybe I need a break!

Within both the financials and finres tables I have resource, budget, eac, actual, month1, month2, month..., month24. I need to add the values of the budget, eac and actual columns and insert them into their respective finres columns.

That is working, however, the problem comes when I try to execute a for loop inside the while loop so that $month1[] is filled with all the different month1 values, and $month2[] is filled with all its values so I can call an array_sum($month1[]) and insert it into the finres table... if that makes sense?

 

here is my code, the trouble starts with the first for loop... unsure (and doubtful) as to whether the proceeding for loops will work

 

//update budget, eac, actual etc into financial resources table
        $sql = "SELECT * FROM " . $projectid . "_financials WHERE resource = '$resource'";
        $addfin = @mysql_query($sql) or die('(F02) Error retrieving financials: ' . mysql_error()); 
        while ($af = mysql_fetch_array($addfin)) {
            $budget[] = $af['budget'];
            $eac[] = $af['eac'];
            $actual[] = $af['actual'];
            
            //insert individual month values into respective arrays
            for($i=1;$i<=24;$i++) {
                $pre = 'month'.$i;
                $$pre[$i] = $af["month$i"];
            //I have tried putting in echo 'bob'; here but it doesn't even print bob.
            }
        }
            //get sums of arrays
            $sum_budget = array_sum($budget);
            $sum_eac = array_sum($eac);
            $sum_actual = array_sum($actual);
            
            for($i=1;$i<=24;$i++) {
                $pre = 'sum_month'.$i;
                $post = 'month'.$i;
                $$pre = array_sum($$post);
            }

            //insert into finres table
            $sql = "UPDATE " . $projectid . "_finres SET
                    budget = '$sum_budget',
                    actual = '$sum_actual',
                    eac = '$sum_eac'";
                    
            for($i=1;$i<=24;$i++) {
                $sum = 'sum_month'.$i;
                $pre = 'month'.$i;
                $sql .= ", ".$$pre." = '".$$sum;
            }
                
            $ok = @mysql_query($sql) or die("(F03) Financials Error: " . mysql_error());    

 

I get the

"Warning: array_sum() [function.array-sum]: The argument should be an array in ..... on line 197" x24

 

In other words the for each loop isn't executing and putting the elements into the array??

 

Please help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.