Pablo
Pablo

Reputation: 113

Sum results inside while loop

I have following code inside while loop (displaying results from database into table):

$count = count($prvi_podatak_ispis); 
for($i = 0; $i < $count - 1; ++$i){
        $sum = $prvi_podatak_ispis[$i] - $prvi_podatak_ispis[$i + 1]; 
        //echo "<font color='red'>" . $sum . '</font><br />';
}

$count2 = count($drugi_podatak_ispis);
for ($k=0; $k < $count2 - 1 ; ++$k) { 
    $sum2 = $drugi_podatak_ispis[$k] - $drugi_podatak_ispis[$k + 1];
    //echo "<font color='blue'>" . $sum2 . '</font><br />';
}

$count3 = count($treci_podatak_ispis);
for ($o=0; $o < $count3 - 1; ++$o) { 
    $sum3 = $treci_podatak_ispis[$o] - $treci_podatak_ispis[$o + 1];
    //echo "<font color='black'>" . $sum3 . '</font><br />';
}

$count4 = count($cetvrti_podatak_ispis);
for ($p=0; $p < $count4 - 1; ++$p) { 
    $sum4 = $cetvrti_podatak_ispis[$p] - $cetvrti_podatak_ispis[$p + 1];
    //echo "<font color='green'>" . $sum4 . '</font><br />'; 
}

If I uncomment echo inside any for loop, results are fine. But I am trying to sum all results foreach displayed result from database like:

$final = $sum + $sum2 + $sum3 + $sum4;

When I use echo $final;, first result is 0 and I get errors Notice: Undefined variable: sum2, sum, sum3, sum4 probably for that first result. Where am I wrong?

Upvotes: 0

Views: 341

Answers (4)

Noor A Shuvo
Noor A Shuvo

Reputation: 2807

Initialize a sum variables to 0 and update this only variable in each for loop

$sum = 0;
$count = count($prvi_podatak_ispis); 
for($i = 0; $i < $count - 1; ++$i){
        $sum = $prvi_podatak_ispis[$i] - $prvi_podatak_ispis[$i + 1]; 
        //echo "<font color='red'>" . $sum . '</font><br />';
}

$count2 = count($drugi_podatak_ispis);
for ($k=0; $k < $count2 - 1 ; ++$k) { 
    $sum+= $drugi_podatak_ispis[$k] - $drugi_podatak_ispis[$k + 1];
    //echo "<font color='blue'>" . $sum2 . '</font><br />';
}

$count3 = count($treci_podatak_ispis);
for ($o=0; $o < $count3 - 1; ++$o) { 
    $sum+= $treci_podatak_ispis[$o] - $treci_podatak_ispis[$o + 1];
    //echo "<font color='black'>" . $sum3 . '</font><br />';
}

$count4 = count($cetvrti_podatak_ispis);
for ($p=0; $p < $count4 - 1; ++$p) { 
    $sum+= $cetvrti_podatak_ispis[$p] - $cetvrti_podatak_ispis[$p + 1];
    //echo "<font color='green'>" . $sum4 . '</font><br />'; 
}

And now, what you need $final=$sum.

Upvotes: 0

Nigel Ren
Nigel Ren

Reputation: 57121

Your only adding the elements in each loop, not creating an overall sum of the values. Set the value to 0 outside the loop and use += to add the value each time...

$sum = 0;
$count = count($prvi_podatak_ispis); 
for($i = 0; $i < $count - 1; ++$i){
        $sum += $prvi_podatak_ispis[$i] - $prvi_podatak_ispis[$i + 1]; 
        //echo "<font color='red'>" . $sum . '</font><br />';
}

Repeat this pattern throughout.

Upvotes: 1

l.b.vasoya
l.b.vasoya

Reputation: 1221

Declare a variable as global like string variable $str="" or integer $i=0 and you can access it's in outside the for

In your code sum1,sum2 etc sum variable are local for in For loop that scope are end at the loop are end

You should declare variable at the global it access

Upvotes: 3

Ramesh
Ramesh

Reputation: 2403

Initialize sum variables globaly like $sum = $sum2 = $sum3 = $sum4 = 0 . It will work

Upvotes: 1

Related Questions