Script multi insert dan update data di codeigniter

Script multi insert dan update data di codeigniter

Hello kawan, postingan ini bukanlah postingan tutorial. Ini hanya postingan arsip doank, saya takut lupa makanya saya langsung tulis di blog seadanya. Maaf kalau penjelasannya tidak terlalu jelas. Untuk pembahasan ini akan saya bahas di postingan selanjutnya :) .

Insert Data
View

<div class="form-group">
<div class="input-group">
<div class="row">
<div class="col-md-6">
<input class="form-control" type="text" name="name_attribut[]" placeholder="Attribut Name" maxlength="20">
</div>
<div class="col-md-6">
<input class="form-control" type="text" name="value_attribut[]" placeholder="Value" maxlength="25">
</div>
</div>
<span class="input-group-btn">
<button class="btn btn-danger" type="button">
<span class="glyphicon glyphicon-minus"></span>
</button>
</span>
</div>
</div>

Controller :

//Update data attribut
$name_attribut= $this->input->post('name_attribut');
$result = array();
foreach($name_attribut AS $key => $val){
$result[] = array(
"nama_attribut" => $_POST['name_attribut'][$key],
"value" => $_POST['value_attribut'][$key]
);
}
$this->db->insert_batch('dt_attribut', $result);

Update Data
View

<?php foreach($attribut as $att){?>
<div class="form-group">
<div class="input-group">
<div class="row">
<div class="col-md-6">
<input class="form-control" type="text" name="name_attribut[]" placeholder="Attribut Name" value="<?php echo $att->nama_attribut;?>" maxlength="20">
<input type="hidden" name="ID_att[]" value="<?php echo $att->ID;?>">
</div>
<div class="col-md-6">
<input class="form-control" type="text" name="value_attribut[]" placeholder="Value" value="<?php echo $att->value;?>" maxlength="25">
</div>
</div>
<span class="input-group-btn">
<button class="btn btn-danger" type="button">
<span class="glyphicon glyphicon-minus"></span>
</button>
</span>
</div>
</div>
<?php } ?>

Controller :

//Update data attribut
$ID_att = $this->input->post('ID_att');
$result = array();
foreach($ID_att AS $key => $val){
$result[] = array(
"ID" => $ID_att[$key],
"nama_attribut" => $_POST['name_attribut'][$key],
"value" => $_POST['value_attribut'][$key]
);
}
$this->db->update_batch('dt_attribut', $result, 'ID');

4/Post a Comment/Comments

  1. iyah,
    berdasarkan angka gimana gan ?

    ReplyDelete
  2. bagaimana jika where nya lebih dari 2

    ReplyDelete
    Replies
    1. Tinggal begini aja,

      $this->db->where("...", "...");
      $this->db->where("...", "...");
      $this->db->where("...", "...");
      $this->db->update_batch('dt_attribut', $result);

      Delete
  3. mas saya mau tanya utk kasus ini :
    $id_proker = $_POST['id_proker'];
    $kd_rek_akun = $_POST['kd_rek_akun'];
    $akun = $_POST['akun'];
    $tahapan = $_POST['tahapan'];
    $ren_biaya = $_POST['ren_biaya'];
    $data3 = array();

    $index=0;
    foreach ($id_proker as $r) {
    array_push($data3, array(
    'id_proker'=>$r,
    'kd_rek_akun'=>$kd_rek_akun[$index],
    'kodrek_akun'=>$akun[$index],
    'tahapan'=>$tahapan[$index],
    'ren_biaya'=>$ren_biaya[$index],
    'status'=>'beban',
    ));
    $index++;
    }

    $this->db->update_batch('tbl_beban',$data3,'id_proker');

    salahnya dimana ya ? soalnya error terus
    Terima kasih sebelumnya

    ReplyDelete

Post a Comment

Previous Post Next Post