【问题标题】:array for multiple where_in condition in codeigniter [duplicate]codeigniter中多个where_in条件的数组[重复]
【发布时间】:2017-08-30 07:28:07
【问题描述】:
$this->db->where('column_field1',$value1);
$this->db->where('column_field2',$value2);

对于以上两个查询,我们可以编写一个查询:

$arr = array('column_field1'=>'value1', 'columne_field2'=>'value2');

function select($arr){
    .. .. ..
    ... .. . ..
    $this->db->where($arr);
}

where_in 查询有没有解决办法:

function($value1, $value2){
   $this->db->where_in('column_field1',$value1);
   $this->db->where_in('column_field2',$value2);
}

我试过但没用:

arr = array('column_field1'=>$arr,'column_field2'=>arr2);

function select_in($arr)
 {
        $this->db->select('*');
        $this->db->from('table');
        $this->db->where_in($arr);
        $query = $this->db->get();
        return $query;
    }

我想结合 where_in 条件,以便我可以为其存储多个 column_field 和数组。 $this->db->where_in($arr); 其中 $arr 包含一对 column_filed 和 array_of_value: $arr = $array('column_field'=>$arr_of_value);

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:
    function select_in($arr)
     {
            $this->db->select('*');
            $this->db->from('table');
            $this->db->where($arr);  // change here
            $query = $this->db->get();
            return $query;
        }
    

    如果你想要多个 where In 那么你需要写两次....在单个语句中是不可能的。

    $this->db->where_in('field1',$cond1);
    $this->db->where_in('field2' , $cond2);
    

    注意:Where_in 类似于 where id IN (1,2,3...),但在您的情况下,您正在执行多个 where 条件。

    【讨论】:

    • 我要合并多个 where_in() 条件
    • 即使我无法在 where_in() 条件下传递数组,例如:$this->db->where_in($arr);
    • 已编辑请检查
    • 不可能?确定..?
    • yap 根据它的source code 这是不可能的
    【解决方案2】:

    创建自定义方法是一个合理的解决方案,您可以扩展数据库的活动记录类或编写自定义函数,该函数采用数组并调用 where_in 如下所示

    function multiple_where_in($array){
        foreach($array as $key  => $data){
            $this->db->where_in($key, $data);
        }
      }
    

    然后像下面这样调用

    function select_in($arr)
    {
            $this->db->select('*');
            $this->db->from('your_table');
            $this->multiple_where_in($arr);  // call local function created above
            $query = $this->db->get();
            return $query;
    }
    
    // create array like below, fieldname and field_values inside array
    $arr = array(
                  'column_field1'=>array('value1','value2'),  
                  'columne_field2'=>array('value2','value3')
    );
    
    // call your select method
    $this->select_in($arr);
    

    【讨论】:

      【解决方案3】:

      您可以尝试这个解决方案来解决您的问题。

          $this->db->select('*');
          $this->db->from('table');
          if(!empty($cond1) && !empty($cond2)){
              $this->db->where("field1 IN (".$cond1.") AND field2 IN (".$cond2.") ",null,false);    
          }
          $query = $this->db->get();
          return $query;
      

      我希望这会对你有所帮助。谢谢!

      【讨论】:

        猜你喜欢
        • 2016-10-13
        • 1970-01-01
        • 1970-01-01
        • 2020-10-22
        • 1970-01-01
        • 2020-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多