phpphpquery

Php sum value once when loop start


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

?>

Solution

  • 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++;
    }