Codeigniter通过传递逗号分隔值来获取数据

时间:2015-01-12 06:52:15

标签: php mysql codeigniter

以下是我的申请详情: 在mysql数据库中,我有products表,其中包含id,description和price。在basket表格中,我有products_id形式1,2,3即逗号分隔值。

现在,在控制器中,我通过此函数从basket表中获取了product_id列表:

$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);
}

我在products_model模型中有这个功能:

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

$ids将products_id回显为1,2,3。现在,如何通过从products_id表中传递product并在篮子中显示来获取每个产品的详细信息?如果我的问题不明确,请放弃评论。

2 个答案:

答案 0 :(得分:1)

希望这可以帮到你

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

答案 1 :(得分:0)

将条件添加到数据库查询中,即

$this->db->where_in('your_column_name',$ids);
相关问题