Hungry Mind
Hungry Mind

Reputation: 226

Codeigniter getting data by passing comma separated values

Here is my application details: In mysql database, I have products table with id, description and price. And in basket table, I have products_id in the form 1,2,3 i.e. comma separated values.

Now, in controller, I got the product_id list from basket table by this function:

$data['baskets'] = $this->baskets_model->get_all();
$pr_id=$this->baskets_model->get_all();

foreach ($pr_id as $pr){            
    $get_pr_info=$this->products_model->get_all($pr->product_id);
}

I have this function in products_model model:

public function get_all($ids=NULL) {
    if($ids !== NULL) {         
        echo $ids;      
    }
    else {
        $query = $this->db->get('products');
        return $query->result_array();
    }
}

The value $ids echoes the products_id as 1,2,3. Now, how to get the details of each product by passing this products_id from product table and show in basket? Please drop comment if my question is not clear.

Upvotes: 0

Views: 3980

Answers (2)

Shaiful Islam
Shaiful Islam

Reputation: 7134

Hope this may help you

public function get_all($ids=NULL) 
{
   $this->db->from('products');

   if($ids !== NULL) 
   {
       $this->db->where("id in ($ids)");//use this
       //$this->db->where_in('id',$ids);//This may work but I think it will not give your right answer 
        //if your id like 1,2,3. this will not work i think but if you send only one id it will work.
   }    
   $query = $this->db->get();
   return $query->result_array();
}

Upvotes: 2

Needhi Agrawal
Needhi Agrawal

Reputation: 1326

Add where condition in to the database query i.e.

$this->db->where_in('your_column_name',$ids);

Upvotes: 0

Related Questions