Php 如何结合CodeIgniter中的where条件和限制
我是新来的。实际上,我想从我的产品表中获取数据。对于多个where条件,即具有指定的菜单id,但对于所有方式,限制应为1。我希望每个菜单id中有一个产品。以下是我的型号,可能是错误的。请帮忙Php 如何结合CodeIgniter中的where条件和限制,php,codeigniter,Php,Codeigniter,我是新来的。实际上,我想从我的产品表中获取数据。对于多个where条件,即具有指定的菜单id,但对于所有方式,限制应为1。我希望每个菜单id中有一个产品。以下是我的型号,可能是错误的。请帮忙 My Model public function home_products(){ $this->db->select('*'); $this->db->from('product'); $this-&
My Model
public function home_products(){
$this->db->select('*');
$this->db->from('product');
$this->db->where('menu_id', 51, 52, 53, 54, 55, 56);
$this->db->where('product_status', 0);
$this->db->limit('1');
$query = $this->db->get();
return $query->result();
}
使用$this->db->where_in('menu_id',数组(51,52,53,54,55,56));就地$this->db->where('menu_id',51,52,53,54,55,56) 试试看:-
$this->db->select('*');
$this->db->where_in('menu_id', array(51, 52, 53, 54, 55, 56));
$this->db->where('product_status', 0);
$this->db->limit(1);
$query = $this->db->get('product');
return $query->result();
有关更多详细信息,请参阅。添加
$this->db->group\u by('menu\u id')代码>和
改变
$this->db->where('menu_id',51,52,53,54,55,56)代码>
到
$this->db->where_in('menu_id',数组(51,52,53,54,55,56))代码>
最终代码如下所示:
public function home_products(){
$this->db->select('*');
$this->db->from('product');
$this->db->where_in('menu_id', array(51, 52, 53, 54, 55, 56));
$this->db->where('product_status', 0);
$this->db->group_by('menu_id');
$this->db->limit('6');
$query = $this->db->get();
return $query->result();
}
我认为您应该删除$this->db->limit('1')代码>并尝试按照@Akash指定的方式进行操作。我尝试了,但现在它显示了与各自菜单相关的所有产品\u id尝试添加$this->db->group\u by('menu\u id')代码>可能会有帮助。它正在工作。。但是由于有6个菜单ID,我需要添加$this->db->limit('6');而且非常感谢..更新了我的答案..删除了$this->db->limit(1)