Php sum value once when loop start

83 views Asked by At

Hi I have a value in variable i want to add this value first time when loop start not always. for example

<?php

   $balance = 100;
   while($row = mysql_fetch_array($query)
   {
      echo $row['amount'] + $balance; // for example $row['amount'] is 50, 20 , 30;
   }
 ?>

i want result as follow

<?php       

   150
   170
   200

?>
2

There are 2 answers

0
Suresh On BEST ANSWER

Sample Code..

$balance = 100;
$counter=1;
$total=0;
while($row = mysql_fetch_array($query)
{
    if($counter==1)
    {
        $total=$row['amount'] + $balance; // for example $row['amount'] is 50, 20 , 30;
    }
    else
    {
        $total = $row['amount']+$total;
    }

    echo $total.'<br>';

    $counter++;
}
0
Robert On

the most simple way to do so

<?php

   $balance = 100;
   while($row = mysql_fetch_array($query)
   {
      $balance = $balance + $row['amount'];
      echo $balance;
   }
 ?>