CodeIgniter Forums

Full Version: Remove this code from the View
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hi there,

I have a table of financial transactions and would like to calculate a running total column. Currently I perform the calculation in the View but I know this isn't correct (I'm pretty new to MVC).

How do I move this code out of the view? Any help would be great. Thanks. Scott


&lt;? $row = 0 ?&gt;
&lt;? $runningTotal = 0 ?&gt;

  &lt;?php foreach($transactions_list as $transaction): ?&gt;

  &lt;?  if ($row == 0)
    $balance = $transaction->amount;
    $balance = ($transaction->amount + $runningTotal);
  <td>&lt;?= $transaction->date ?&gt;</td>
     <td>&lt;?= $transaction->memo ?&gt;</td>
     <td>&lt;?= $transaction->amount ?&gt;</td>
     <td>&lt;?= $balance ?&gt;</td>


&lt;? $runningTotal = $runningTotal + $transaction->amount ?&gt;
&lt;?php $row++ ?&gt;
    &lt;?php endforeach ?&gt;