2015-12-03 9 views
6

Ho una tabella in HTML come questo: VISTAPHP Codeigniter aggiornamento batch su qualche campo fallito

<table id="tableReport" class="table table-hover"> 
<thead> 
    <tr> 
     <th>NO</th> 
     <th>TYPE</th> 
     <th>ITEM</th> 
     <th>DAMAGE</th> 
     <th>REPAIR</th> 
     <th>REMARKS</th> 
     <th>MANHOUR</th> 
     <th>MATERIAL</th> 
     <th>A/C</th> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
     <td> 
      <input class="form-control" type="text" value="68" placeholder="68" disabled="" name="name_type"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="Cleaning" placeholder="Cleaning" disabled="" name="name_item"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="Certificate" placeholder="Certificate" disabled="" name="name_item"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="Broken" placeholder="Broken" disabled="" name="name_damage"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="Blast&Paint" placeholder="Blast&Paint" disabled="" name="name_repair"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="AAAAAA" placeholder="AAAAAA" disabled="" name="name_remarks"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="10.00" placeholder="10.00" disabled="" name="name_damage"> 
     </td> 
     <td> 
      <input id="material" class="form-control" type="text"> 
     </td> 
     <td> 
      <input id="A/C" class="form-control" type="text"> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <input class="form-control" type="text" value="69" placeholder="69" disabled="" name="name_type"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="Cleaning" placeholder="Cleaning" disabled="" name="name_item"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="Exterior" placeholder="Exterior" disabled="" name="name_item"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="Modified" placeholder="Modified" disabled="" name="name_damage"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="Replace" placeholder="Replace" disabled="" name="name_repair"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="BBBBB" placeholder="BBBBB" disabled="" name="name_remarks"> 
     </td> 
     <td> 
      <input class="form-control" type="text" value="10.00" placeholder="10.00" disabled="" name="name_damage"> 
     </td> 
     <td> 
      <input id="material" class="form-control" type="text"> 
     </td> 
     <td> 
      <input id="A/C" class="form-control" type="text"> 
     </td> 
    </tr> 
</tbody> 
</table> 

jQuery ho ottenuto tutto il valore su coloro tabella utilizzando questo jQuery:

$('#tableReport').find('tbody').find('tr').each(function() { 
     var row_data = []; 
     $(':input', this).each(function() { 
      row_data.push($(this).val()); 
     }); 
     table_data.push(row_data); 
}); 

Il risultato era il seguente:

Array 
(
[0] => Array 
    (
     [0] => 68 
     [1] => Cleaning 
     [2] => Certificate 
     [3] => Broken 
     [4] => Blast&Paint 
     [5] => AAAAAA 
     [6] => 10.00 
     [7] => a 
     [8] => b 
    ) 

[1] => Array 
    (
     [0] => 69 
     [1] => Cleaning 
     [2] => Exterior 
     [3] => Modified 
     [4] => Replace 
     [5] => BBBBB 
     [6] => 10.00 
     [7] => c 
     [8] => d 
    ) 

) 

Questo array è usato per update_batch nella mia tabella.

mysql> select * from tb_repair_detail; 
+-----------+--------------------+------+-----------+-----------+---------+---------+----------+------+ 
| DETAIL_ID | REPAIR_ESTIMATE_ID | ITEM | DAMAGE_ID | REPAIR_ID | REMARKS | MANHOUR | MATERIAL | AC | 
+-----------+--------------------+------+-----------+-----------+---------+---------+----------+------+ 
|  68 |     43 | 01 | 01  | 30  | AAAAAA | 10.00 |  NULL | NULL | 
|  69 |     43 | 03 | 16  | 45  | BBBBB | 10.00 |  NULL | NULL | 
+-----------+--------------------+------+-----------+-----------+---------+---------+----------+------+ 
2 rows in set (0.00 sec) 

SO, AJAX ottenere chiamare il controllore prendere questa azione:

$.ajax({ 
     url: "<?php echo base_url('admin/c_admin/update_json_detail'); ?>", 
     type: "POST", 
     data: { 
      POST_ARRAY: table_data 
     }, 
     dataType: 'json', 
     success: function (obj) { 
      console.log(obj); 
     } 
    }); 
return false; 

CONTROLLER

public function update_json_detail(){ 
    $execute = $this->input->post("POST_ARRAY"); 
    /*CODE TO INSERT BATCH*/ 
    $callback = $this->m_admin->update_eir_to_cost($execute, execute_first_index[0]); 
} 

Questo è il modello.

public function update_eir_to_cost($data, $id) { 
    $this->db->trans_start(); 
    $this->db->update_batch('tb_repair_detail', $data, $id); 
    $this->db->trans_complete(); 

    if ($this->db->trans_status() === FALSE) { 
     // generate an error... or use the log_message() function to log your error 
     echo "Error Updating"; 
    } 
} 

Il mio grosso problema è che voglio utilizzare il lotto di aggiornamento. Ma, voglio solo aggiornare manhour e ac field. Ho davvero bloccato per giorni, ogni aiuto è così apprezzato

AGGIORNAMENTO Enorme, grazie a Mr.Sultan. Ora, il mio codice è si presentava così:

public function update_json_detail() { 
    $post_data = $this->input->post("POST_ARRAY"); 
    $execute = array(); 
    foreach ($post_data as $data) { 
     $execute[] = array(
      'ID'=> $data['0'], 
      'MATERIAL' => $data['7'], 
      'AC' => $data['8'] 
     ); 
    } 

    /* CODE TO INSERT BATCH */ 
    $callback = $this->m_admin->update_eir_to_cost($execute); 
} 

Il mio modello ottenere qualche problema, coz, ho bisogno di tre parametri per update_batch

public function update_eir_to_cost($id, $material, $ac) { 
    $data = array(
     "MATERIAL" => $material, 
     "AC" => $ac 
    ); 

    $this->db->trans_start(); 
    $this->db->where($id); 
    $this->db->update_batch('tb_repair_detail', $data); 
    $this->db->trans_complete(); 

    if ($this->db->trans_status() === FALSE) { 
     // generate an error... or use the log_message() function to log your error 
     echo "Error Updating"; 
    } 
} 

Grazie per la soluzione

+0

Che cosa ha a che fare con [tag: batch-file]? – aschipfl

risposta

0

nel controller, sostituisci la tua funzione con questa:

public function update_json_detail(){ 
    $post_data = $this->input->post("POST_ARRAY"); 
    $execute = array(); 
    foreach($post_data as $data){ 
    $execute[] = array(
    'MANHOUR' => $data['6'], 
    'AC' => $data['8'] 
    ); 
    } 
    /*CODE TO INSERT BATCH*/ 
    $callback = $this->m_admin->update_eir_to_cost($execute, execute_first_index[0]); 
} 
1

Ci sono s metodi everal per far fronte a un problema, ma dobbiamo selezionare solo il metodo più intelligente.

È necessario selezionare solo gli elementi richiesti che inserire la matrice e pubblicarli. Fornisci gli attributi del nome agli elementi di input e in view (html) e in jQuery place check per selezionare gli elementi richiesti. Non modificare nulla nel modello e nel controller.

Step # 1 L'elemento di input deve avere attributo nome.

<input class="form-control" type="text" value="10.00" placeholder="10.00" disabled="" name="MANHOUR"> <input id="A/C" name="A/C" class="form-control" type="text" value="11111">

Passo # 2 Jquery - Dove si ottiene valori posto seguente codice al posto di questo row_data.push($(this).val());.

if($(this).attr("name")==='name_type' || $(this).attr("name")==='MANHOUR' || $(this).attr("name")==='A/C'){ 
row_data.push($(this).val());} 
Problemi correlati