CodeIgniter Active Record multiple where and and or statements

I have the following Active Record query.

//Example 1 public function info($school, $class, $student, $keyword) { $this->db->where('school.id', $school); $this->db->where('class.id', $class); $this->db->where('student.id', $student); $this->db->or_where('school.description', $keyword); $this->db->or_where('class.description', $keyword); $this->db->or_where('student.description', $keyword); return $this->db->get('info')->result(); } 

I want to group the bottom 3 "or_where" statements so that they are included in the top 3 words "where". The solution I came up with was ...

 //Example 2 public function info($school, $class, $student, $keyword) { $this->db->where('school.description', $keyword); $this->db->where('school.id', $school); $this->db->where('class.id', $class); $this->db->where('student.id', $student); $this->db->or_where('class.description', $keyword); $this->db->where('school.id', $school); $this->db->where('class.id', $class); $this->db->where('student.id', $student); $this->db->or_where('student.description', $keyword); $this->db->where('school.id', $school); $this->db->where('class.id', $class); $this->db->where('student.id', $student); return $this->db->get('info')->result(); } 

This works fine, but is there any way to do this without repeating the code?

+8
php codeigniter
source share
2 answers

I have found a solution!

 public function info($school, $class, $student, $keyword) { $this->db->where('school.id', $school); $this->db->where('class.id', $class); $this->db->where('student.id', $student); $this->db->where("(school.description LIKE '$keywords' OR class.description LIKE '$keywords' OR student.description LIKE '$keywords')"); return $this->db->get('info')->result(); } 
+12
source share

I just stumbled upon this and want to add the following solution that uses the active syntax CI $this->db->like() :

 public function info($school, $class, $student, $keyword) { $this->db->where('school.id', $school); $this->db->where('class.id', $class); $this->db->where('student.id', $student); $this->db->like('school.description', $keyword); $this->db->or_like('class.description', $keyword); $this->db->or_like('student.description', $keyword); return $this->db->get('info')->result(); } 
+1
source share

All Articles