Combining 'Where' and 'Like' Statements by Using the Ci Activerecords

Combining `where` and `like` statements by using the CI activerecords

You can try this.

   $query = $this->db->select('*')
->from('a')
->where('row',1)
->where("(other_row LIKE '%%' OR another_row LIKE '%%' )")
->get();


foreach ($query->result() as $row) {
//do sth.
}

You can write custom query string (from active record class)

 Custom string:
You can write your own clauses manually:

$where = "name='Joe' AND status='boss' OR status='active'";

$this->db->where($where);

Codeigniter combine where_in and like active record queries

Do you want to find all the friends? If there are only one id, then you don't need like part, as you already found your friend. On the other part, if you not sure about id of your friends, and just want to find all friends matching your like criteria, you may remove where_in part.

This will find your friends:

$this->db->select('id, email, first_name, last_name, current_location_state, current_location, avatar, avatar_fb');
$this->db->from('users');
$this->db->like('first_name', $search);
$this->db->or_like('last_name', $search);
$this->db->or_like("CONCAT(first_name, ' ', last_name)", $search);
$this->db->or_like('email', $search);

Considering there's only one id, such query will find only one friend:

$this->db->select('id, email, first_name, last_name, current_location_state, current_location, avatar, avatar_fb');
$this->db->from('users');
$this->db->where_in('id', $ids);

EDIT

Sometimes, with codeigniter, it is better to use a raw query:

$this->db->query('
SELECT `id`, `email`, `first_name`, `last_name`, `current_location_state`, `current_location`, `avatar`, `avatar_fb`
FROM `users`
WHERE (
`first_name` like ?
or `last_name` like ?
or concat(`first_name`, ' ', `last_name`) like ?
or `email` like ?)
AND `id` in('
.join(',',
array_map(function($e) { return (int) $e; }, $ids))
.')',
"%$search%", "%$search%", "%$search%", "$search")->result();

CodeIgniter Active Record multiple where and or statements

I 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();
}

multiple where condition codeigniter

you can use an array and pass the array.

Associative array method:
$array = array('name' => $name, 'title' => $title, 'status' => $status);

$this->db->where($array);

// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'

Or if you want to do something other than = comparison

$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);

$this->db->where($array);

Codeigniter LIKE with wildcard(%)

$this->db->like() automatically adds the %s and escapes the string. So all you need is

$this->db->like('title', $query);
$res = $this->db->get('film');

See the CI documentation for reference: CI 2, CI 3, CI 4



Related Topics



Leave a reply



Submit