我正在为我的工作场所构建一个登录表单,用户可以在其中登录并记录他们的工作时间。
我这样做是为了让用户进入时间表并可以单击与数据的每个登录和退出时间段相关的相应按钮,例如早间休息,所有按钮都使用更新,开始一天的第一个按钮是插入按钮,它创建包含今天日期的记录,当前时间,按下每个按钮都会更新该记录与当天相关的新时间。
插入按钮(“登录”)每次都会创建一条新记录,因此需要禁用,因此我尝试使用模型函数来检查是否存在包含员工 ID 和日期的记录,如果不存在则执行正常插入为当天的新记录,如果不这样做,我希望它返回类似 FALSE 的内容,我可以在 Controller 中检查它,将其指向不同的页面而不是标准的成功页面。
这是我的模型函数:我猜测我需要返回一个变量,但不太确定。
function clock_in($id, $time, $date) {
$check -> db -> get_where('timesheet',array('datez' => $date, 'staff_id' =>$id));
if ($check -> num_rows() > 0) {
$data = array('id' => null, 'staff_id' => $id, 'clock_in_time' => $time, 'clock_out_time' => null, 'break1_out' => null, 'break1_in' => null, 'lunch_out' => null, 'lunch_in' => null, 'break2_out' => null, 'break2_in' => null, 'datez' => $date);
$this -> db -> insert('timesheet', $data);
} else {
return $check;
}
}
这是我的 Controller :
function clock_in() {
$session_data = $this -> session -> userdata('logged_in');
$data['id'] = $session_data['id'];
$data['time'] = date("h:i");
$data['date'] = date("y-m-d");
$this -> timesheet -> clock_in($data['id'], $data['time'], $data['date']);
$this -> load -> view('timesheet/Clock_in');
}
我还没有尝试过这一部分,我不确定如何检查该变量是否为空,然后加载不同的 vue 并且不插入任何内容。
所有解决方案都很感激,我只需要正确的方法来做到这一点。
最佳答案
进行以下更改:
function clock_in($id, $time, $date) {
$check -> db -> get_where('timesheet',array('datez' => $date, 'staff_id' =>$id));
$data = array();
if ($check -> num_rows() > 0) {
$data = array('id' => null, 'staff_id' => $id, 'clock_in_time' => $time, 'clock_out_time' => null, 'break1_out' => null, 'break1_in' => null, 'lunch_out' => null, 'lunch_in' => null, 'break2_out' => null, 'break2_in' => null, 'datez' => $date);
$this -> db -> insert('timesheet', $data);
}
return $data; // this will be either empty or will contain data
}
在 Controller 中:
function clock_in() {
$session_data = $this -> session -> userdata('logged_in');
$data['id'] = $session_data['id'];
$data['time'] = date("h:i");
$data['date'] = date("y-m-d");
$data = $this -> timesheet -> clock_in($data['id'], $data['time'], $data['date']);
if(empty($data)){
$this -> load -> view('timesheet/Clock_xyz'); // View to load when no data is returned
}else {
$view['data'] = $data
$this -> load -> view('timesheet/Clock_in', $view); // view to load when data is returned
}
}
关于php - CI 如何处理不返回任何内容的查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26887983/