简体   繁体   English

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

[英]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. 这是我的应用程序详细信息:在mysql数据库中,我有具有ID,描述和价格的products表。 And in basket table, I have products_id in the form 1,2,3 ie comma separated values. basket表中,我具有1,2,3形式的products_id ,即逗号分隔的值。

Now, in controller, I got the product_id list from basket table by this function: 现在,在控制器中,我通过以下函数从购物篮表中获得了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);
}

I have this function in products_model model: 我在products_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. $ids值将$ids回显为1,2,3。 Now, how to get the details of each product by passing this products_id from product table and show in basket? 现在,如何通过传递此获取每个产品的细节products_idproduct表,并显示在篮下? Please drop comment if my question is not clear. 如果我的问题不清楚,请发表评论。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM