Like Query Is Not Working properly

644 views Asked by At

In Code igniter Model I am using like query to fetch all the products having first name rice which is not working controller while using get_where('name') it works fine.

  public function fetchdeal_products($id) {
        $this->db->select('*');
        $this->db->from('products');
        $q = $this->db->like("name", $id);

        if ($q->num_rows() > 0) {
        foreach (($q->result()) as $row) {
        $data[] = $row;
        }

        return $data;
         }    
        }

I am using like query to fetch all the products having first name rice which is not working controller while using get_where('name') it works fine. //Controller

        function ajaxdealcategorydata($id = NULL) {
        $this->sma->checkPermissions('index');
        $id = $this->input->get('id');

        $subcategories = $this->pos_model->getdealcategory($id);
        $scats = '';
        foreach ($subcategories as $category) {
        $scats .= "<button id=\"subcategory-" . $category->id . "\"    type=\"button\" value='" . $category->id . "' class=\"btn-prni subcategory\" ><img src=\"assets/uploads/thumbs/" . ($category->image ? $category->image : 'no_image.png') . "\" style='width:" . $this->Settings->twidth . "px;height:" . $this->Settings->theight . "px;' class='img-rounded img-thumbnail' /><span>" . $category->name . "</span></button>";
  }

$products = $this->ajaxdealproducts($id);

if (!($tcp = $this->pos_model->products_count($id))) {
    $tcp = 0;
}

echo json_encode(array('products' => $products, 'subcategories' => $scats, 'tcp' => $tcp));

}

I am using like query to fetch all the products having first name rice which is not working controller while using get_where('name') it works fine.

   function ajaxdealproducts() {
     $this->sma->checkPermissions('index');
     if ($this->input->get('id') ) {
    $id = $this->input->get('id');
    } else {
    $category_id = $this->pos_settings->default_category;
    }
    if ($this->input->get('subcategory_id')) {
    $subcategory_id = $this->input->get('subcategory_id');
    } else {
    $subcategory_id = NULL;
    }
    if ($this->input->get('per_page') == 'n') {
    $page = 0;
    } else {
    $page = $this->input->get('per_page');
}

     $this->load->library("pagination");

     $config = array();
     $config["base_url"] = base_url() . "pos/ajaxdealproducts";
     $config["total_rows"] = $subcategory_id ? $this->pos_model-    >products_count($id, $subcategory_id) : $this->pos_model->products_count($id);
     $config["per_page"] = $this->pos_settings->pro_limit;
     $config['prev_link'] = FALSE;
     $config['next_link'] = FALSE;
     $config['display_pages'] = FALSE;
     $config['first_link'] = FALSE;
     $config['last_link'] = FALSE;

     $this->pagination->initialize($config);

    $products = $this->pos_model->fetchdeal_products($id, $config["per_page"], $page);
    $pro = 1;
    $prods = '<div>';

    foreach ($products as $product) {
    $count = $product->id;
    if ($count < 10) {
        $count = "0" . ($count / 100) * 100;
    }
    if ($category_id < 10) {
        $category_id = "0" . ($category_id / 100) * 100;
    }

    $prods .= "<button id=\"product-" . $category_id . $count . "\" type=\"button\" value='" . $product->code . "' title=\"" . $product->name . "\" class=\"btn-prni btn-" . $this->pos_settings->product_button_color . " product pos-tip\" data-container=\"body\"><img src=\"" . base_url() . "assets/uploads/thumbs/" . $product->image . "\" alt=\"" . $product->name . "\" style='width:" . $this->Settings->twidth . "px;height:" . $this->Settings->theight . "px;' class='img-rounded' /><span>" . character_limiter($product->name, 15) . "</span></button>";

    $pro++;
}

$prods .= "</div>";

if ($this->input->get('per_page')) {
    echo $prods;
} else {
    return $prods;
}

}

4

There are 4 answers

0
shankar kumar On

you have to write it as bellow.

public function fetchdeal_products($id) {
    $this->db->like("name", $id);

    $q = $this->db->get('products');
    if ($q->num_rows() > 0) {
        foreach (($q->result()) as $row) {
            $data[] = $row;
        }

        return $data;
    }    
}
0
Craig On

The database LIKE function only creates a LIKE in the SQL statement. You'll still need to call the GET mothod afterwards; Like this;

$this->db->like('name', $id);
$q = $this->db->get();
0
Eustace On

You are using $this->db_like() incorrectly.

$this->db->like('title', 'match'); 
// Produces: WHERE title LIKE '%match%'

(from documentation)

In your case the usage would be

$this->db->like('name', 'rice')

More info

0
Abdulla Nilam On
public function fetchdeal_products($id) 
{
    $query = $this->db->query("Select products.* From products Where (products.name Like '%$id%')");
    $result = $query->result_array();
    return $result;
}