Codeigniter - Update Multiple Row With Select2

孤者浪人 提交于 2019-12-13 20:37:29

问题


When entering 2 values collecting_id attribute and 2 values nama_driver attribute, and in database successfully input 2 rows,

INPUT--> RESULT


but if inputting 2 values collecting_id and 1 value nama_driver, then only 1 row entered into database ..

INPUT--> RESULT

if I input 2 or more collecting_id and 1 nama_driver, only 1 row in database. I want 2 row or more in database with same nama_driver value

Here is my CONTROLLER :

$collecting_id = $this->input->post('collecting_id');
$nama_driver = $this->input->post('nama_driver');

$data = array();
for ($i = 0; $i < count($collecting_id); $i++) {
    $data[] = array(
        'nama_driver' => $nama_driver[$i],
        'collecting_id' => $collecting_id[$i]    
    );
    }

$this->db->update_batch('process', $data, 'collecting_id');

{
$this->session->set_flashdata('msg','<strong>Data Deliveries Berhasil Diupdate.');
redirect('delivery/deliver_list'); 
}

And here is my VIEW :

 <div class="col-md-6 col-sm-6 col-xs-12">
    <div class="form-group">
    <label>Paket</label>

<select class="js-example-basic-multiple form-control" name="collecting_id[]" multiple="multiple">
<?php 
   if(@$paket_incoming) :
      foreach ($paket_incoming as $row) :
      ?>
<option value="<?php echo $row->collecting_id; ?>"><?php echo $row->collecting_id; ?></option>
<?php
   endforeach;
      endif;
      ?>
</select>
</div></div>

<div class="col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<label>Nama Driver</label>
<select class="js-example-basic-multiple form-control" name="nama_driver[]" multiple="multiple">
<?php 
   if(@$driver) :
      foreach ($driver as $row) :
         ?>
<option value="<?php echo $row->nama; ?>"><?php echo $row->nama; ?></option>
<?php
   endforeach;
      endif;
      ?>
</select>
</div></div>

<div class="col-md-12">
<center><button type="submit" class="btn btn-primary">Simpan</button>
                    <a href="<?php echo base_url();?>delivery/delivery_list"  class="btn btn-default">Batal</a></center>
                    </div></div>

回答1:


You just need to change this

 for ($i = 0; $i < count($collecting_id); $i++) {
    $data[] = array(
        'nama_driver' => $nama_driver[$i],
        'collecting_id' => $collecting_id[$i]    
    );
    }

to this

 for ($i = 0; $i < count($collecting_id); $i++) {
           $tempdata = array(
        'nama_driver' => $nama_driver[$i],
        'collecting_id' => $collecting_id[$i]    
        );
        array_push($data, $tempdata);
        }

use array_push . your array format was not same as CI expected to be.
then you can use : $this->db->update_batch('process', $data, 'collecting_id');



来源:https://stackoverflow.com/questions/53044207/codeigniter-update-multiple-row-with-select2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!