0

I have retrieved some values from database. In my view file I have the following:

 $row['fee_amount']; 

Now, I want to sum up all the values inside $row['fee_amount']; and then show it.

I know I could sum up when querying the database, but I am interested to learn how to add using PHP .

Would you please kindly teach me how to do it?

EDIT

    <?php if(count($records) > 0) { ?>              
        <table id="table1" class="gtable sortable">
            <thead>
                <tr>                        
                    <th>S.N</th>
                    <th>Fee Type</th>
                    <th>Fee Amount</th>                         
                </tr>
            </thead>
            <tbody>             
                <?php $i = 0; foreach ($records as $row){ $i++; ?>
                    <tr>
                        <td><?php echo $i; ?>.</td>
                        <td><?php echo $row['fee_type'];?></td>                                                
                        <td><?php echo $row['fee_amount'];?></td>                                           
                    </tr>                                                                                          
                <?php  } ?>             
            </tbody>                
            <tr>                                                        
                <td></td>
                <td>Total</td>
                <td> 
                    I WANT TO DISPLAY THE SUMMATION RESULT HERE ADDING UP VALUES INSIDE THIS>>> <?  $row['fee_amount']; ?> 
                </td>                                                                               
            </tr>               
        </table>
    <?php } ?>
2
  • What is your concrete problem? How to sum? How to get all fee_amount elements from an array? I guess if you add more of the code you've already done it's more clear. Commented Nov 27, 2011 at 14:42
  • Thanks Hakre for you reply. Let me describe the problem. I have fetched data from my database table, and you know when you fetch information from database things come as an array. Now in my view file (I am using Codeigniter) I want to sum up all the values that exist inside $row['fee_amount'];. and then display it. But I don't know how to sum. I am posting my view file above; please check it :) Thanks Commented Nov 27, 2011 at 15:06

4 Answers 4

3

In your view file, with your foreach loop, add a $sum variable next to your $i counter and add the amount per each iteration (similar to like you increase $i):

<?php
    $i = 0;
    $sum = 0;
    foreach ($records as $row)
    { 
        $i++; 
        $sum += $row['fee_amount']; ?>

(I put this over multiple lines to make it more readable).

After the foreach has finished, $sum contains the total amount:

        <td>Total: <?php echo $sum; ?></td>

That simple it is. You only need a new variable ($sum) and do the calculation.

Sign up to request clarification or add additional context in comments.

Comments

2

Use a loop

$sum = 0;
while($row...){
  $sum += $row['fee_amount']
}

echo $sum;

Comments

0

You could use;

$someValue = 0;
foreach($row["fee_amount"] as $value) {
  $someValue = $someValue + $value;
}

1 Comment

I'd guess that $row['fee_amount'] is not an array but a single integer value.
0

using this php function, if $row['fee_amount'] is an array ^_^

for example:

$a = array(2, 4, 6, 8);
array_sum($a)

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.