如何求和任何foreach循环结果的总值

时间:2017-05-12 07:47:42

标签: php codeigniter

我想显示总薪资,这是所有员工净薪水的总和。我怎样才能做到这一点?我尝试了一些代码,但它没有用 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>

这是我尝试过的净薪水

的代码
<?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; ?> 

0 个答案:

没有答案