2015-12-14 59 views
1

我使用Codeigniter 2,我想更新多行。Codeigniter:更新多行

型號

這裏是我的模型,但是,問題是,第一行只更新。

foreach ($this->input->post('qty') as $key => $value) { 
      $data = array(
        'item_quantity' => $value, 
        'discount' => $this->input->post('discount')[$key], 
        'senior' => $this->input->post('qty')[$key] 
      ); 

      $this->db->where('item_id', $this->input->post('item_id')[$key]); 
      $this->db->where('request_id', $this->input->post('request_id')); 
      return $this->db->update('medical_request_items', $data); 
     } 

另一方面,我設法做到了這一點。

foreach ($query->result() as $row) 
            { 

             echo '<tr>'; 
             echo '<input type="hidden" name="item_id[]" value="' . $row->item_id . '">'; 
             echo '<td>' . $x++ . '</td>'; 
             echo '<td>' . $row->item_id . '</td>'; 
             echo '<td>' . $row->item_name . '</td>'; 
             echo '<td><input type="text" size="1" name="qty[]" value="' . $row->item_quantity . '"></td>'; 
             echo '<td>' . $row->item_retailprice . '</td>'; 
             echo '<td><input type="text" size="2" name="discount[]" value="' . $row->discount . '"></td>'; 
             echo '<td><input type="text" size="2" name="senior[]" value="' . $row->senior . '"></td>'; 
             echo '<td>' . ($row->item_quantity * $row->item_retailprice) . '</td>'; 
             echo '<td>' . (($row->item_quantity * $row->item_retailprice)-($row->item_quantity * $row->discount)-($row->item_quantity * $row->senior)) . '</td>'; 
             echo '</tr>';             
            } 

我試圖回顯/打印,它工作正常。但它不適用於多個更新。

編輯

所以我設法尋找出update_batch的是,我有一個錯誤。

$data = array(
        foreach ($this->input->post('qty') as $key => $value) { 
          array(
           'request_id'  => $this->input->post('request_id'), 
           'item_id'   => $this->input->post('item_id')[$key], 
           'item_quantity' => $value, 
           'discount'  => $this->input->post('discount')[$key], 
           'senior'   => $this->input->post('senior')[$key] 
          ) 
        } 
       ); 



     $this->db->update_batch('medical_request_items', $data, 'item_id, request_id'); 
+0

$ _POST包含什麼?請顯示結構。 – DFriend

回答

0

您發佈多個陣列,但在你的模型,你只能通過一個循環,所以你的where子句將永遠是相同的。

+0

我必須再次循環嗎? –

0

你絕對可以每次更新一條記錄。但我認爲應該也可以使用更新批次一次完成所有這些操作。

$this->db->update_batch('yourTableName', $updateData, 'field_to_use_for_key'); 

爲Cl 2 http://www.codeigniter.com/userguide2/database/active_record.html

====

確定的信息可以深入到代碼

// define an array 
    $requests = array() ; 


    foreach ($this->input->post('qty') as $key => $value) { 

    // notice the brackets, very important $requests[] 
    // this will add to the same array in each loop 

     $requests[] =  array(
      'request_id'  => $this->input->post('request_id'), 
       'item_id'   => $this->input->post('item_id')[$key], 
       'item_quantity' => $value, 
       'discount'  => $this->input->post('discount')[$key], 
       'senior'   => $this->input->post('senior')[$key] 
          ) ; 


        } 


    // for testing only -- echo out your array so you can confirm its all good 
    echo 'here is requests array: <br><pre>' ; 

    print_r($requests) ; 

    echo '</pre>' ; 


// use one field to act as the key 
$this->db->update_batch('medical_request_items', $requests, 'request_id'); 

因此多數民衆贊成使用一個密鑰。如果你需要更多的一鍵或更新的條件 - 那麼只需在foreach循環中更新即可。當然,您也可以在更新之前驗證任何用戶數據

+0

我在如何設置項目的數組()時遇到困難。請檢查帖子。我已經更新了 –

+0

新的代碼示例在上面 – cartalot