-1

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

?>
4
  • 2
    This is most basic PHP. Take a tutorial first Commented Apr 14, 2017 at 10:52
  • thanks buddy :) got the result. Commented Apr 14, 2017 at 11:01
  • use if condition with some variable like if($i == 1) and increment $i++; at the last in the if condition Commented Apr 14, 2017 at 11:02
  • Your question is not clear. the result should be 150, 20, 30 not as your regarded! Commented Apr 14, 2017 at 11:15

2 Answers 2

0

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++;
}
Sign up to request clarification or add additional context in comments.

Comments

0

the most simple way to do so

<?php

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.