phpcodeigniter

Codeigniter Query with Where with two arrays


hope you have good day.

I'm modifiying some code of Login function. It's in Codeigniter 3. So the username can be filled with email too. How to write this code with Codeigniter?

Here's some code of my controller:

$data = array('username' => $this->input->post('username', TRUE),
            'password' => md5($this->input->post('password', TRUE))
        );

$data1 = array('email' => $this->input->post('username', TRUE),
            'password' => md5($this->input->post('password', TRUE))
        );

$this->load->model('m_login'); 
        $result= $this->m_login->cek_user($data, $data1);

        if ($result->num_rows() == 1) {
some code......

and here's the function of cek_user() in my Model:

public function cek_user($data, $data1) {
    $query = $this->db->get_where('mytable', $data||$data1);
    return $query;
}

all i want to ask is, how to use OR condition with array in $data anda $data1 Thanks


Solution

  • According to the codeigniter Manual, you can write your queries in below methods, Heres you can use where and or_where functions. You can find more on the below link.

    Link

    Method 1

    public function cek_user($data, $data1) {
    
                $where = "(name='$data[name]' AND password ='$data[password]') OR (email='$data1[email]' AND password ='$data[password]')";
                $this->db->where($where);
                $query = $this->db->get('my_table');
                return $query;
    
    }
    

    Method 2

    public function cek_user($data, $data1) {
    
                $this->db->where($data);
                $this->db->or_where($data1); 
                $query = $this->db->get('my_table');
                return $query;
        $query = $this->db->get_where('mytable  ', $data||$data1);
        return $query;
    
    }