As we know, codeigniter query builder is one of the easiest method to create database queries. But in some complex situations it is not giving exact results. If codeigniter got upgraded, this type of errors will be solved, but it was not upgraded for many years. Anyhow today we are going to see about "How to use or_like and where condition in codeigniter"
or_like and where condition in codeigniter |
or_like and where condition in codeigniter
Table name : items | |||
id | Name | Type | Country |
---|---|---|---|
1 | Banana | Fruit | India |
2 | Carrot | Vegetable | Pakistan |
3 | Apple | Fruit | USA |
4 | Tomato | Vegetable | UAE |
5 | Orange | Fruit | Iran |
6 | cucumber | Vegetable | India |
Consider above table "items". Now you need to search a data from name and country fields, where type = Fruit. In this case use following code to get exact result from codeigniter.
public function search_items($search_keyword="") {
$this->db->select('*');
$this->db->from('items');
$where = "type = 'Fruit' AND (name LIKE '%$search_keyword%' OR country LIKE '%$search_keyword%')";
$this->db->where($where);
$query = $this->db->get();
if($query->num_rows() > 0) {
return $query->result();
}
return false;
}
Try this and if you have any doubts or any alternative solutions, please comment below. Thank you - W3schools100 Team
No comments:
Post a Comment