2015-12-14 143 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