Codeigniter:更新多行


Codeigniter: updating multiple rows

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

型号

这是我的模型,但问题是,第一行只更新了。

 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'); 

您发布了多个数组,但在您的模型中,您只循环通过一个数组,因此where子句将始终相同。

您肯定可以foreach并一次更新一条记录。但我认为也应该能够使用更新批处理一次完成所有这些操作。

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

ci 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'); 

所以这只需要一把钥匙。如果您需要不止一个键或where条件来进行更新,那么只需在foreach循环中进行更新即可。当然,在更新之前,您还需要验证任何用户数据。