【问题标题】:How can i give a where condition before update_batch in codeigniter?如何在 codeigniter 中的 update_batch 之前给出 where 条件?
【发布时间】:2013-05-04 07:12:00
【问题描述】:

我想更新我的表,其中输入相同的输入字段名称是数组并且有 添加更多生成输入字段的函数,如下所示:

我想在 codeigniter 中做 update_batch 我的模型我创建了一个这样的函数: 这是代码块:

    function update_batch_all($tblname,$data=array(),$userid)
    {
       $this->db->trans_start();
       $this->db->where('userid',$userid);
       $this->db->update_batch($tblname,$data);
       $this->db->trans_complete();
       return TRUE;
    }

它不工作。 任何人都可以帮助我如何使用具有 where 条件的更新批处理来更新表数据?

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    您可以阅读update_batch() here 的文档

    以下是简短摘要:

    您传入一个关联数组,其中包含您的 where 键和更新值。作为 update_batch() 调用的第三个参数,您指定 assoc 数组中的哪个键应用于 where 子句。

    例如:

    $data = array(
      array(
        'user_id' => 1,
        'name' => 'Foo'
      ), array(
        'user_id' => 2,
        'name' => 'Bar'
      )
    );
    
    $this->db->update_batch($tbl, $data, 'user_id');
    

    传递的参数分解$tbl 是表名。 $data 是关联数组。 'user_id' 告诉 CI,$data 中的 user_id 键是 where 子句。

    上述查询的效果:user_id = 1 的用户名设置为Foouser_id=2 的用户名设置为Bar

    在您的情况下,如果您想在每个数组中设置相同的 user_id 键与您的 data 数组,您可以做一个快速的 for 循环:

    foreach ($data as &$d) { 
      $d['user_id'] = $user_id;
    }
    
    
    $this->db->update_batch($tbl, $data, 'user_id');
    

    【讨论】:

      【解决方案2】:
      You can use,
      
            $this->db->update_batch($tblname,$data,'user_id');
      

      但数据中的所有数组都必须有一个字段'user_id'

      例如:

          $data=array( 
      
                     array('user_name'=>'test1','user_id'=>1),
                     array('user_name'=>'test2','user_id'=>2)
      
                        );
      

      您可以从here获取有关 update_batch 的更多详细信息

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多