用条件Codeigniter计数并插入

时间:2018-09-12 01:24:39

标签: php codeigniter time-and-attendance

我有一张表格需要填写,以填写津贴表。津贴表包含员工编号,员工姓名,时间段和出勤天数。该表格仅包含员工代码,员工姓名和时间段。因此需要通过计算出勤表中根据时间段确定的出勤天数来自动填写出勤天数。

这是出勤表的结构:

|  id_attend   |  employee_id  |  employee_name  |   date   |  time_in  |  time_out  |
|      1       |     RYN       |     RYAN        |01.01.2011|  07.00    |  19.00     |

同时这是津贴表的结构

|  id_allowance   |  employee_id  |  employee_name  |   period  |  days_attended   |
|      1          |     RYN       |     RYAN        |  01.2011  |     1            |

因此,day_attended应该通过根据employee_id和期间计算出多少工作日来填写。我在模型中尝试了这个:

        public function count_days_attended()
    {
             $this->db->select('*, COUNT(attendance.date) as allowance.days_attended');
             $this->db->from('attendance');
             $this->db->join('allowance','attendance.employee_id= allowance.employee_id');
             $this->db->order_by('employee_id','DESC');
             $this->db->where(MONTH('attendance.date'), MONTH('allowance.period'));
             $query = $this->db->get();
             return $query->result();
    }

这是我的控制器:

public function submit(){
    $dateTime = new DateTime($this->input->post('period'));
    $formatted_date = date_format($dateTime, 'Y-m-d' );

    $data = array(
        'employee_id'=>$this->input->post('employee_id'),
        'employee_name'=>$this->input->post('employee_name'),
        'period'=> $formatted_date,
    );
    $this->absengaji_m->count_days_attended;
    $this->absengaji_m->insert($data);
    redirect('absengaji');
   }

没有错误,但是程序甚至没有对我的代码做出任何响应。我点击提交后,其出席天数仍然显示为0

有什么办法可以解决这个问题? :/

1 个答案:

答案 0 :(得分:2)

我认为您在这里有几个问题。为了使它们嘎嘎作响,

  1. 您未在WHERE子句中指定哪个员工
  2. 您没有正确调用方法
  3. 您没有捕获方法的返回结果
  4. 您没有将结果追加到插入查询中。

我不是CodeIgniter开发人员,所以我无法为您提供特定的语法,但是类似的事情应该可以使您走上正轨,

//accept a parameter so you can know which employee
public function count_days_attended( $employee_id ) {
     $this->db->select('*, COUNT(attendance.date) as allowance.days_attended');
     $this->db->from('attendance');
     $this->db->join('allowance','attendance.employee_id= allowance.employee_id');
     $this->db->order_by('employee_id','DESC');
     $this->db->where(MONTH('attendance.date'), MONTH('allowance.period'));
     //add the employee id to your where clause so you are searching for the correct employee only
     //$this->db->where()   employee id = $employee_id
     $query = $this->db->get();
     return $query->result();   //you are returning a result here
}

public function submit(){
    $dateTime = new DateTime($this->input->post('period'));
    $formatted_date = date_format($dateTime, 'Y-m-d' );

    $data = array(
        'employee_id'=>$this->input->post('employee_id'),
        'employee_name'=>$this->input->post('employee_name'),
        'period'=> $formatted_date,
    );

    //save the result in a variable
    $days_attended = $this->absengaji_m->count_days_attended( $data['employee_id'] );  //call as a method and pass the employee id to the method

    //append to insert query
    $data['days_attended'] = $days_attended;

    $this->absengaji_m->insert($data);
    redirect('absengaji');
}