How can I add the total of multiple variables?
<p><strong>Problem</strong></p>

<p>I have three records in my database so I want to add up those records by checking if it's a profit or loss and after getting the total from each record I want to make it a grand total by adding all the totals from each record whereas currently It is just showing me the total for the last record. Here's how the data in the database looks like after running the loop and getting the data.</p>

<p>$transact_money (1) = 10.00|10.00|10.00|;
$pnl_results (1) = Profit|Loss|Profit|;</p>

<p><strong>total should be calculated = 10.00</strong> </p>

<p>$transact_money (2) = 20.00|15.00|;
$pnl_results (2) = Profit|Loss|;</p>

<p><strong>total should be calculated = 5.00</strong> </p>

<p>$transact_money (3) = 30.00|35.00|10.00|;
$pnl_results (3) = Profit|Profit|Loss|;</p>

<p><strong>total should be calculated = 55.00</strong> </p>

<p>Grand total should be <strong>70.00</strong> but right now as I said taking only the last runned loops data and printing <strong>55.00</strong></p>

<p><strong>Code</strong></p>

<pre><code>$sql = "SELECT * FROM profitnloss WHERE username='".$username."' and currency='$contract_id' and t_name not like'%$t_name%' order by id";
            //echo $sql;
            $result = $db1-&gt;getResult($sql);
            if(mysql_num_rows($result)&lt;=0){
                echo "&lt;br /&gt;You are not playing in any other games.";
            }else{
                echo "&lt;table border='0' bordercolor='#036' cellpadding='5' cellspacing='0' align='center' width='640' class='tborder'&gt;";
                while ($row = mysql_fetch_assoc($result)) {

                    $profit = 0;
                    $grand_total = 0;
                    $loss = 0;

                    $transact_money = $row['pnl_amount'];
                    $pnl_results = $row['pnl'];
                    $t_name = $row['t_name'];

                    $pnl_amnt = "0.00";
                    $pnl_result = "Started";
                    $pnl_dates = substr($row['date'], 0, 10);
                    //echo count($pnl_amount);

                    $transact_money = explode("|", $transact_money);
                    $pnl_results = explode("|", $pnl_results);
                    for($i=0; $i&lt;count($transact_money); $i++){
                        if($pnl_results[$i]=='Profit'){
                            $profit = $profit + $transact_money[$i];
                        }else{
                            $loss = $loss + $transact_money[$i];
                        }//end if
                    }//end for..
                    $money_results_total = $profit - $loss;
                    $grand_total = $money_results_total;
                }//end while..
</code></pre>
