How to sum the total cost of any result of a foreach loop

I want to show Total Salary, which is the sum of all employees' salaries. How can i do this? I tried some code but didn't work 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 the code i tried for net salary amounts

<?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; ?> 

      

+3


source to share


1 answer


I think you are using data tables to display records in a table. Try to print Total after finishing calculating records.

The snapshot shows the total before the process ends.

Also change a 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;

      

However, if you do not find a complete attempt to validate the data inside $emp_salary_info

, it may have blank or incorrect salary data.

+1


source







All Articles