简体   繁体   English

Codeigniter:更新多行

[英]Codeigniter: updating multiple rows

I'm using Codeigniter 2 and I wanted to update multiple rows. 我正在使用Codeigniter 2,并且想更新多行。

Model 模型

Here is my model, but, the problem is, the first row only is updated. 这是我的模型,但是问题是,仅更新了第一行。

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

On the other side, I managed to do this. 另一方面,我设法做到了。

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

I tried to echo/print, and it is working fine. 我试图回显/打印,并且工作正常。 But it doesn't work on multiple update. 但这不适用于多次更新。

EDIT 编辑

So I manage to look out the update_batch yet, I have an error. 所以我设法找出了update_batch但是我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子句将始终相同。

you can definitely foreach and update them one record at a time. 您绝对可以foreach并一次更新一条记录。 but i think should also be able to use update batch to do all of them at once. 但我认为也应该能够使用更新批处理一次完成所有操作。

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

info for ci 2 http://www.codeigniter.com/userguide2/database/active_record.html ci 2的信息http://www.codeigniter.com/userguide2/database/active_record.html

==== ====

ok lets dig into the code 好,让我们深入研究代码

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

so thats using one key. 这样就可以使用一把钥匙。 if you need more then one key or where conditions to do the update - then just update in the foreach loop. 如果您需要一个以上的键或更新条件,请在foreach循环中进行更新。 Also you are of course validating any user data before updating . 当然,您还需要在更新之前验证所有用户数据

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

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