Monday, April 11, 2016

How to use or_like and where condition in codeigniter query builder

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

or_like and where condition in codeigniter

Table name : items
idNameTypeCountry
1BananaFruitIndia
2CarrotVegetablePakistan
3AppleFruitUSA
4TomatoVegetableUAE
5OrangeFruitIran
6cucumberVegetableIndia

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