Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何结合CodeIgniter中的where条件和限制_Php_Codeigniter - Fatal编程技术网

Php 如何结合CodeIgniter中的where条件和限制

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-&

我是新来的。实际上,我想从我的产品表中获取数据。对于多个where条件,即具有指定的菜单id,但对于所有方式,限制应为1。我希望每个菜单id中有一个产品。以下是我的型号,可能是错误的。请帮忙

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)