code troubles!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joel24
Forum Newbie
Posts: 10
Joined: Tue Feb 03, 2009 6:18 am

code troubles!

Post by joel24 »

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, i'll highlight the bit where the trouble starts, unsure (and doubtful) as to whether the proceeding for loops (in green) will work

Code: Select all

//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'];
            
            [color=#FF0000]//insert individual month values into respective arrays[/color]
            [color=#FF0000]for($i=1;$i<=24;$i++) {[/color]
                $pre = 'month'.$i;
                $$pre[$i] = $af["month$i"];
[color=#FF0000]I have tried putting in echo 'bob'; here but it doesn't even print bob.[/color]
            }
        }
            //get sums of arrays
            $sum_budget = array_sum($budget);
            $sum_eac = array_sum($eac);
            $sum_actual = array_sum($actual);
            
            [color=#408000]for($i=1;$i<=24;$i++) {
                $pre = 'sum_month'.$i;
                $post = 'month'.$i;
                $$pre = array_sum($$post);
            }   [/color]    
 
            //insert into finres table
            $sql = "UPDATE " . $projectid . "_finres SET
                    budget = '$sum_budget',
                    actual = '$sum_actual',
                    eac = '$sum_eac'";
                    
            [color=#408000]for($i=1;$i<=24;$i++) {
                $sum = 'sum_month'.$i;
                $pre = 'month'.$i;
                $sql .= ", ".$$pre." = '".$$sum;
            }[/color]
                
            $ok = @mysql_query($sql) or die("(F03) Financials Error: " . mysql_error());   
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: code troubles!

Post by susrisha »

have you tried checking the number of rows returned from the query?
If the number of rows is 0, it wont even enter the loop you are trying to access..

Code: Select all

 
mysql_num_rows($adfin);
 
try this statement before the

Code: Select all

while($result=mysql_fetch_array($adfin))
statement
Post Reply