2017-07-28 54 views
-1

我有字典作爲笨:在笨更新多行的一個聲明

array(
 
    'ID1' => 1 , 
 
    'ID2' => 2, 
 
    'Status' => 'Done' 
 
)

雖然我在我的表,身份證和狀態只有2列。我想僅使用一次($ this-> db-> update)更新ID爲1和2的兩行。是否有可能或者我需要自定義查詢。 我想要這樣做($ this-> db-> update(其中ID1 == 1 & & ID2 == 2))。

回答

0

要麼

// Get your status 
$item = array('Status' => $response['Status']); 

// unset status 
unset($response['Status']); 

// I assume except status rest all values are ids so 
// array_values gives id 
$this->db->where_in('id', array_values($response)); 

// Update table 
$this->db->update('table', $item); 

OR

您必須修改陣列

$data = array(
     array(
     'id' => 1 , 
     'Status' => 'Done' 
     ), 
     array(
     'id' => 2 , 
     'Status' => 'Done' 
     ) 
    ); 

然後

$this->db->update_batch('table_name', $data, 'id'); 

這樣你就可以修改

(評:親愛的,我不能這樣做,因爲我得到一些服務響應。我無法編輯此回覆。 )

[[email protected] tmp]$ cat test.php 
<?php 
$response = array(
    'ID1' => 1 , 
    'ID2' => 2, 
    'Status' => 'Done' 
); 

$data = array(); 
$item = array('Status' => $response['Status']); 
unset($response['Status']); 

foreach($response as $new){ 
     $item['id'] = $new; 
     $data[] = $item; 
} 
print_r($data); 

// Here you update 
// $this->db->update_batch('table_name', $data, 'id'); 
?> 

輸出

[[email protected] tmp]$ php test.php 
Array 
(
    [0] => Array 
     (
      [Status] => Done 
      [id] => 1 
     ) 

    [1] => Array 
     (
      [Status] => Done 
      [id] => 2 
     ) 

) 
+0

親愛的我不能這樣做,因爲我得到一些服務響應。我無法編輯此回覆。 – user6159419

+0

@ user6159419請參閱我的編輯,由此您可以開始 –

+0

謝謝親愛的。 。 – user6159419

0
//this will update the values where ids equal any value in the second were_in parameter 

//data is array of values to update 
$data = [ 
    'Status'=>'some status', 
]; 

//array of ids to update 
$ids = [1,2]; 

$this->db->where_in('id', $ids)->update('table_name', $data);