2017-03-29 3 views
0

Ich habe nach Wegen gesucht, aber passende Speckle finden. so, ich habe Tisch Produkt und Ich zeige zusammen mit seiner ID. und ich legte eine Form an. und wenn ich die Update-Taste drücke, dann was in der Form ihrer entsprechenden ID aktualisiert wurde.Codeigniter: wie multipleple Datensatz zu aktualisieren, wo ID

meiner Sicht:

<?php echo form_open('stock/updating_stock');?> 
    <div class="panel-body scroll-menu" style="margin-top:0px;height:170px;padding-top:5px;"> 
    <?php foreach($critical_plus_warning as $data){?> 
     <input type="hidden" name="id_product[]" value="<?php echo $data->id_product;?>"> 
     <h5 class="hidden-xs"><b><?php echo $data->product_name;?></b> <input type="text" class="pull-right" style="width:10%;margin-left:10px;text-align:center;" name="update_stock[]" value="<?php echo $data->stock?>"> 
     <?php 
     $stock = $data->stocks; 
     if($stock < 10){ 
     echo "<label style='font-size:13px;' class='label label-danger pull-right'>$stock</label>"; 
     }else{ 
     echo "<label style='font-size:13px;' class='label label-warning pull-right'>$stock</label>"; 
     } 
     ?> 
     </h5> 
     <h5 class="hidden-lg"><b><?php $limited_word = word_limiter($data->product_name,3); echo $limited_word; ?></b> <input class="pull-right" type="text" style="width:10%;margin-left:10px;text-align:center;" name="update_stock[]" value="<?php echo $data->stocks?>"> 
     <?php 
     $stock = $data->stocks; 
     if($stock < 10){ 
     echo "<label style='font-size:13px;' class='label label-danger pull-right'>$stock</label>"; 
     }else{ 
     echo "<label style='font-size:13px;' class='label label-warning pull-right'>$stock</label>"; 
     } 
     ?> 
     </h5> 
    <?php }?> 
    </div> 
    <div class="col-xs-12" style="margin-top:-5px;background-color:white;padding:6px;background-color:white;box-shadow:0px 0px 8px 0px #bababa;"><button type="submit" name="submit" class="btn btn-success">Save data</button></div> 
    <?php echo form_close();?> 

und mein Controller:

function updating_stock(){ 

    $id = $this->input->post('id_product'); 
    $stok = $this->input->post('update_stock'); 
    $user = $this->data['id']; 

    for($i=0;$i<count($id);$i++){ 
    $data = array(
     array(
     'id'  => $id, 
     'stok'  => $stok, 
     'diubah' => $user, 
     'tgl_diubah'=> date('Y:m:d H:i:s'), 
     ), 
    ); 
    } 
    //print_r($data); 
    $this->stok_adm->update_stok($data); 
} 

und meine Modelle:

function update_stok($data){ 
    $this->db->update_batch($this->table, $data,'id_product'); 
    return $this->db->affected_rows(); 
} 

Antwort

0

Sie brauchen eine where-Klausel:

$this->db->where('id', $id); 

... denke ich.

0

Versuchen Sie folgendes:

-Controller

function updating_stock(){ 

$id = $this->input->post('id_product'); 
$stok = $this->input->post('update_stock'); 
$user = $this->data['id']; 

for($i=0;$i<count($id);$i++){ 
$data = array(
    array(
    'id'  => $id, 
    'stok'  => $stok, 
    'diubah' => $user, 
    'tgl_diubah'=> date('Y:m:d H:i:s'), 
    ), 
); 
} 
//print_r($data); 
$this->stok_adm->update_stok($data,$id); 

}

Modell:

function update_stok($data,$id){ 
$this->db->update_batch($this->table, $data,'id_product'); 
return $this->db->affected_rows(); 

}

0

Ich denke, in Ihrem Beispiel update_ba tch erzeugt falsche Abfrage, weil Sie jeden Datensatz in db aktualisieren müssen, anstatt sie gemeinsam zu aktualisieren - update_batch ersetzt Werte in mehreren Zeilen, die Bedingungen erfüllen, so dass auch falsche Zeilen aktualisiert werden können (Beispiel in documentation). Bessere Idee ist es, jedes einzelne Produkt mit seiner ID zu aktualisieren. Es könnte so aussehen:

Modell

function update_stok($data, $id_product){ 
    $this->db->where('id_product', $id_product); 
    return $this->db->update($this->table, $data); 
} 

-Controller

function updating_stock(){ 
    $ids = $this->input->post('id_product'); 
    $stok = $this->input->post('update_stock'); 
    $user = $this->data['id']; 

    foreach($ids as $key => $id_product) { 
     $data = array(
      'stok'  => $stok[$key], 
      'diubah' => $user, 
      'tgl_diubah'=> date('Y:m:d H:i:s'), 
     ); 
     $this->stok_adm->update_stok($data, $id_product); 
    } 
} 
0
$data = array(
    array(
     'title' => 'My title' , 
     'name' => 'My Name 2' , 
     'date' => 'My date 2' 
    ), 
    array(
     'title' => 'Another title' , 
     'name' => 'Another Name 2' , 
     'date' => 'Another date 2' 
    ) 
); 

$this->db->update_batch('mytable', $data, 'title'); 

http://www.codeigniter.com/user_guide/database/query_builder.html?highlight=update_batch

+0

so sollte Ihr Code sein: for ($ i = 0; $ i $ id, 'stok' => $ stok, 'diubah' => $ user, 'tgl_diubah' => Datum ('Y: m: d H: i: s'), ), ); } // print_r ($ data); $ this-> stok_adm-> update_stok ($ data); – maxlen

Verwandte Themen