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



基本上,我必须根据每个员工的月出勤率来计算他们的月餐费,这只剩下一张桌子,考勤表

餐费的计算非常简单,我只需要计算员工参加的天数,并用一个数字计算(这个数字应该是动态的或可编辑的(

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

这是考勤表的数据

----------------------------------------------------------------------------
| 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();
}

如果记录存在则更新,如果不存在则插入(假设上一张图中有规格的单独表格(

最新更新