在使用CI插入之前实现计数数据

时间:2018-01-24 03:01:29

标签: php mysql codeigniter

我正在制作一份OffDayWork表格,我希望每位员工只能申请他们的非工作时间最多12次。我使用this作为参考,但它仍然没有用。

这是我的结构表:

- tabel_cuti

    holID | holEmpId | holName |
    ---------------------------| 
    1     | 1        | Ied     | 
    ---------------------------| 
    2     | 3        | exp1    | 
    ---------------------------| 
    3     | 4        | exp2    | 
    ---------------------------| 
    4     | 1        | Married | 
    ---------------------------|

我在我的模型中试过这个

  • EmployeeModel

    public function HolidayRegister($data)
    {
        $this->db->insert('tbl_cuti',$data);
        return true;
    }
    
    public function HitungCuti($data)
    {       
    $this->db->select('count(table_cuti.holID) as countcuti');
    $this->db->from('tabel_cuti');
    $this->db->join('tabel_pegawai','tabel_cuti.holEmpId = tabel_pegawai.empID');
    $this->db->group_by('table_cuti.holEmpId');
    $totalhol = $this->db->get();
    }
    

我的控制员(员工)

public function HolidayRegister()
    {
        if(!empty($_POST))
        {
            $data =  array('holEmpId' => $this->input->post('employee_id'),
                    'holName' => $this->input->post('holiday_name'),
                    'holDate' => $this->input->post('holiday_date'),
                    'holDescription' => $this->input->post('description'),
                    'created_date' => date('Y-m-d H:i:s'),
                    );
            $result = $this->EmployeeModel->HitungCuti($data);

            if($result -> num_rows() < 12)
            {
                $this->EmployeeModel->HolidayRegister($data);
            }
            else
            {
                $data =  array('holEmpId' => $this->input->post('employee_id'),
                    'holName' => $this->input->post('holiday_name'),
                    'holDate' => $this->input->post('holiday_date'),
                    'holDescription' => $this->input->post('description'),
                    'created_date' => date('Y-m-d H:i:s'),
                    );
                $this->session->set_flashdata('SUCCESSMSG','Tidak ada kesempatan cuti');
                $data['getEmployee'] = $this->EmployeeModel->getEmployee();
                $this->load->view('holiday_register',$data);
            }


        }
        else
        {
            $this->session->set_flashdata('SUCCESSMSG', "Holiday Register Successfully!!");
            redirect('view-holiday');
        }
    }

这是我第一次尝试CI,所以我需要你的帮助。

1 个答案:

答案 0 :(得分:0)

HitungCuti()方法中,您正在计算所有员工假期数据。您只需要为提交的员工数据限制选择:

public function HitungCuti($data)
{       
    $this->db->select('holID');
    $totalhol = $this->db->get_where('tabel_cuti', array('holEmpId' => $data['holEmpId'])); // only search for the specified employee ID

    return $totalhol;
}