Reputation: 15
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
Upvotes: 1
Views: 227
Reputation: 1795
you can do this in a single line of query
public function cek_user($data, $data1) {
$this->db->where($data); $this->dB->or_where($data1);
$query = $this->db->get('mytable');
return $query->result();
}
or your function should work like this
public function cek_user($data, $data1) {
$query = $this->db->get_where('mytable', $data,$data1);
return $query->result();
}
Hope it helps you out
Upvotes: 1
Reputation: 522
try this :-
$username = $this->input->post('username', TRUE);
$password = $this->input->post('password', TRUE);
$where = "(`username`='$username' AND `password`='$password' or `email`='$username' AND `password`='$password')";
$this->db->where($where);
$query = $this->db->get('table');
return $query;
Upvotes: 0
Reputation: 379
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.
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;
}
Upvotes: 3
Reputation: 136
In Your controller just put below code
$this->load->model('m_login');
$result= $this->m_login->cek_user($this->input->post());
if (!empty($result)) {
//Do Stuff
}
In Your Model
public function cek_user($postData) {
$this->db->where('username', $postData['username']);
$this->db->or_where('email', $postData['email']);
$this->db->where('password', md5($postData['password']));
$query = $this->db->get('mytable')->result();
return $query;
}
Hope this will help you.
Upvotes: 0