如何使用数据库中的列进行计算并插入结果 Codeigniter

How to do a calculation with a column in database and insert the result Codeigniter

基本上我要根据每个员工的出勤率来计算每个员工每月的伙食费,这样我就只剩下一个table,出勤率table

伙食费的计算很简单,我只需要计算员工出勤的天数,然后用一个数字来计算(这个数字应该是动态的或者editable)

this is the example of the calculation:
number of attendance * 15.00 (editable number)

这是我的出勤率数据table

----------------------------------------------------------------------------
| attendance_id | emp_code | emp_name  |    date      | time_in | time_out |
|       1       |    ALB   |  Albert   | 2018.01.01   | 07.00   |  18.00   |
|       2       |    GlN   |  GLENN    | 2018.01.01   | 07.00   |  18.00   |
|       3       |    ALB   |  Albert   | 2018.01.02   | 07.00   |  18.00   |
|       4       |    GLN   |  GLENN    | 2018.01.02   | 07.00   |  18.00   |
|       5       |    ALB   |  Albert   | 2018.01.04   | 07.00   |  18.00   |
|       6       |    GLN   |  GLENN    | 2018.01.04   | 07.00   |  18.00   |
----------------------------------------------------------------------------

到目前为止,我已经算出了每月的回顾(通过计算他们参加了多少天),这是我目前的状况:

------------------------------------------------
| emp_code   |   emp_name   |  days_attended    |
| ALB        |   Albert     |         3         |
| GLN        |   GLENN      |         3         |
-------------------------------------------------

这是我的控制器

    public function index(){
    $this->load->model('allowance_m');
    $data['query'] = $this->allowance_m->calculate();
    $this->load->vars($data);
    $this->load->view('allowance_v',$data);
    }

这是我的模型 (allowance_m.php)

public function hitungabsen()
    {
        $this->db->select('attendance_id,emp_code,emp_name, COUNT(date) as days_attended');    
        $this->db->from('attendance');
        $this->db->group_by('emp_code');
        $query = $this->db->get();
        return $query->result();
    }

这是我的视图allowance_v.php

<table class="table table-striped table-bordered table-hover">
                <thead>
                    <tr>
                        <th><center>Employee Code</center></th>
                        <th><center>Employee Name</center></th>
                        <th><center>Days Attended</center></th>
                    </tr>
                </thead>

                <?php 
                    $no = 1;
                    foreach($query as $row){
                    ?>
                    <tr>
                        <td><?php echo $row->emp_code ?></td>
                        <td><?php echo $row->emp_name ?></td>
                        <td><?php echo $row->days_attended ?></td>
                    </tr>
                <?php } ?>          
                </table>

现在我真的需要知道如何根据输入的出勤天数计算膳食津贴,计算结果后,我需要将结果插入数据库,以便我可以制作一份 pdf 报告。结果应该是这样的:

------------------------------------------------------------------------
| emp_code   |   emp_name   |  days_attended    |     meal allowance    |
| ALB        |   Albert     |         3         |(automaticaly updated) |
| GLN        |   GLENN      |         3         |(automaticaly updated) |
------------------------------------------------------------------------

提前致谢!真的需要尽快的答复,这对我来说意义重大

不是很了解所有内容,但这应该行得通:

public function calc() {
    $meal_allowance = array_column($this->db->select('emp_code')->get('tablename')->result_array(), 'emp_code');
    $n = 15;
    $results = $this->hitungabsen();
    $this->db->trans_start();
    foreach ($results as $row) {
        $calc = intval($row->days_attended) * $n;
        if (in_array($row->emp_code, $meal_allowance)) {
            $this->db->update('tablename', array('meal_allowance' => $calc));
        } else {
            $data = array(
                'emp_code' => $row->emp_code,
                'emp_name' => $row->emp_name,
                'days_attended' => $row->days_attended,
                'meal_allowance' => $calc
            );
            $this->db->insert('tablename', $data);
        }
    }
    $this->db->trans_complete();
    return $this->db->trans_status();
}

如果记录存在则更新,如果不存在则插入(假设 table 与上一张图中的规格分开)