简体   繁体   English

使用简单数组在Codeigniter中进行批量更新

[英]batch update in codeigniter with simple array

I want to do a batch update in codeignter and pass the array data, instead of running multiple queries. 我想在codeignter中进行批量更新并传递数组数据,而不是运行多个查询。

Array ( [439] => 0 [440] => 0 [441] => 0 [442] => 1 [443] => 0 )

The 439, 440, 441, 442, 443 are the id's and the 0, 0, 0, 1, 0 are the values that need to be put into the active column. 439、440、441、442、443是id,而0、0、0、1、0是需要放入活动列中的值。

I can achieve this running it thru a loop, but I want to do a batch update. 我可以通过循环来运行它,但是我想进行批量更新。

foreach($this->input->post($field_name) as $key => $value)
{
    $insert = array(
        'id'        =>  $key,
        'active'    =>  $value,
    );

    $this->db->where('id', $key)->update($table, array('active' => $value));
}   

Build the array with a foreach loop, then use the batch update. 使用foreach循环构建数组,然后使用批处理更新。

EDIT: I forgot the second array() in the opening array. 编辑:我忘了在开始数组中的第二个array() It may or may not be needed. 可能需要也可能不需要。

$data = array(array());

foreach($this->input->post($field_name) as $key => $value)
{
    $push = array(
        'id'        =>  $key,
        'active'    =>  $value,
    );
array_push($data, $push);
}
$this->db->update_batch('mytable', $data, 'id');

loop through your array and use an associative array to store the data 遍历数组并使用关联数组存储数据

foreach($this->input->post($field_name) as $key => $value)
{
  $insert[] = array(
     'id'        =>  $key,
     'active'    =>  $value
  );

}   
$this->db->update_batch('yourtable', $insert, 'id');

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

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