Search code examples
phpcodeigniteractiverecord

Get the total of variables in active records


Ok, I have a variable called $total_bal that is the answer of a simple equation made from two queries stored in variables x and y

for example

$y = $row->amount_one;
$z = $row->amount_two;

$total_bal = $z + $y;

However I have many entries in amount_one and amount_two.

As I am using codeigniters active records

I tried

echo $this->db->count_all($total_bal);

But this dose not work, any idea of the best way to do this ?

So Im after a way to add all the $total_balup, for more incite into my code see bellow.

<?php
if (isset($records)) : foreach ($records as $row) :


    $x = $row->amount_two;
    $y = $row->ammount_one;

   $total_bal = $z + $y;
   ?>

    <table>
        <tbody>
        <tr>
            <td>amount one</td>
            <td>amount two</td>
        </tr>

        <tr>
            <td>
               <?php echo $x;?>
            </td>

            <td>
               <?php echo $y;?>
            </td>
            <td>
               <?php echo $$total_bal;?>
            </td>
        </tr>

<!--        <tr>-->
<!--            <td>-->
<!--               --><?php //echo $this->db->count_all('$total_bal'); ?>
<!--            </td>-->
<!--        </tr>-->
        </tbody>
    </table>

<?php endforeach; ?>

<?php else : ?>
    <h3>You Have No Accounts</h3>
    <h4>Why No Add A Account?</h4>
<?php endif; ?>

Solution

  • One way to do this is to use an accumulator variable. Take for example, a $grandTotal variable. You set it to 0 outside the foreach, over each iteration of the loop, you add the $rowTotal to the $grandTotal. Eventually when the loop ends, you have a total value of all row totals.

    The benefit to this method is that it doesn't require any additional calls to the database and since you are already looping through the values to display them, accumulating them is minimal processing.

    <?php if (isset($records)) : ?>
    <table>
      <thead>
        <tr>
          <th>Amount One</th>
          <th>Amount Two</th>
          <th>Total</th>
        </tr>
      </thead>
      <tbody>
      <?php $grandTotal = 0; ?>
      <?php foreach ($records as $row) : ?>
        <?php 
          // Add field values to get row total
          $rowTotal = $row->amount_one + $row->amount_two;
        ?>
        <?php 
          // Add row total to grand total
          $grandTotal += $rowTotal;
        ?>
        <tr>
            <td>
               <?php echo $row->amount_one;?>
            </td>
    
            <td>
               <?php echo $row->amount_two;?>
            </td>
            <td>
               <?php echo $rowTotal;?>
            </td>
        </tr>
        <?php endforeach; ?>
        <tr>
          <td></td>
          <td></td>
          <td><?php $grandTotal; ?></td>
        </tr>
      </tbody>
    </table>
    <?php else : ?>
      <h3>You Have No Account</h3>
      <h4>Why Not Add An Account?</h4>
    <?php endif;?>