【问题标题】:implemented counting data before insert using CI在使用 CI 插入之前实现计数数据
【发布时间】:2018-01-24 03:01:28
【问题描述】:

我正在制作一个 OffDayWork 表单,我希望每个员工最多只能申请 12 次休假。我使用this 作为我的参考,但它仍然没有工作。

这是我的结构表:

- tabel_cuti

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

我在我的模型中尝试过这个

  • 员工模型

    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,所以我需要你的帮助。

【问题讨论】:

  • 解决了吗??
  • @AbdullaNilam 还没有,有什么建议吗?

标签: php mysql codeigniter


【解决方案1】:

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

【讨论】:

  • 抱歉,我之前没有真正彻底阅读整个问题,我已经更新了我的答案,我删除了 count..asjoingroup_by,因为它是不必要的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多