LEARNER
LEARNER

Reputation: 81

How to sum total value of any foreach loop result

I want to show Total Salary, which is the sum of all employee's net salary. How can I do that? I have tried some code but its not working Please check this image

            <tbody>
                <?php 

                $salary = 0;
                if (!empty($emp_salary_info)):foreach ($emp_salary_info as $v_emp_salary):

                $salary = $salary + $v_emp_salary->basic_salary;
                 ?>                    
                        <tr>
                            <td><?php echo $v_emp_salary->employment_id; ?></td>
                            <td><?php echo $v_emp_salary->first_name . ' ' . $v_emp_salary->last_name ?></td>
                            <td><?php echo $gross = $v_emp_salary->basic_salary + $v_emp_salary->house_rent_allowance + $v_emp_salary->medical_allowance + $v_emp_salary->special_allowance + $v_emp_salary->fuel_allowance + $v_emp_salary->phone_bill_allowance + $v_emp_salary->other_allowance ?></td>
                            <td><?php echo $deduction = $v_emp_salary->tax_deduction + $v_emp_salary->provident_fund + $v_emp_salary->other_deduction ?></td>
                            <?php $net_salary = $gross - $deduction ?>

                            <td><?php echo $net_salary ?></td>
                            <td><?php
                                if ($v_emp_salary->employment_type == 1) {
                                    echo 'Provision';
                                } else {
                                    echo 'Permanent';
                                }
                                ?></td>
                            <td><?php echo btn_view('admin/payroll/view_salary_details/' . $v_emp_salary->employee_id); ?></td>
                            <td>
                                <?php echo btn_edit('admin/payroll/manage_salary_details/' . $v_emp_salary->employee_id . '/' . $v_emp_salary->designations_id); ?>                                    
                            </td>
                        </tr>                
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>

this is code I have tried for sum the net salary

<?php           
    $salary = 0;
    if (!empty($emp_salary_info)):foreach ($emp_salary_info as $v_emp_salary):
        $salary = $salary + $v_emp_salary->basic_salary;

        echo 'Total Salary :' 
?> 
<?php echo $salary; ?> 

Upvotes: 3

Views: 605

Answers (1)

Saurabh Parekh
Saurabh Parekh

Reputation: 449

I think you are using data-tables for displaying table records. Try to print Total after completing record calculations.

In image you are displaying total before completing process.

Also change few lines of code

    if(!isset($v_emp_salary->basic_salary) || empty($v_emp_salary->basic_salary) || !is_numeric($v_emp_salary->basic_salary))
    {
        $v_emp_salary->basic_salary = 0;
    }

$salary = $salary + $v_emp_salary->basic_salary;

Still if you do not find total try to check data inside $emp_salary_info it may have empty or wrong data with salary.

Upvotes: 1

Related Questions