Codeigniter - Update Multiple Row With Select2

2019-08-20 01:22发布

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条回答
劫难
2楼-- · 2019-08-20 01:55

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

查看更多
登录 后发表回答