foreach($q->;result()作为$row)在结果集中循环两次


foreach($q->result() as $row) is looping twice through result set

我的foreach循环似乎循环了两次!我在我的CodeIgniter项目模型中有这个:

public function check_late_in($late){
    $this->db->select('users.title, users.firstname, users.lastname, users.email_address, clock_in_out.user_id, clock_in_out.loggedin');
    $this->db->join('users', 'clock_in_out.user_id = users.id');
    $this->db->where('UNIX_TIMESTAMP(loggedin) >= ', strtotime($late));
    $q = $this->db->get('clock_in_out');
    foreach($q->result() as $row){
        $data = array();
        $data = array(
            'user_id'  => $row->user_id,
            'assignee' => 88,
            'description' => 'You were LATE!',
            'created'     => date('Y-m-d H:i:s')
        );
        $q = $this->db->insert('notifications', $data);
    }
    if($q){
        return true;
    }
    return false;
}

如果我在查询后转储$q->result(),我会得到2个结果。然而,在循环中,数据是重复的,所以我在执行后得到的不是2行数据,而是4行数据。

您需要在foreach循环开始之前声明$data = array()

我找到了原因,在我的控制器中我调用了

if($this->method){
    $data['foo'] = $this->method;
}

所以函数被调用了两次。

谢谢你的帮助!