2015-12-03 15 views
6

Tôi có một bảng trên html như thế này: XEMPHP CodeIgniter hàng loạt cập nhật trên một số lĩnh vực thất bại

<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 tôi có tất cả các giá trị trên những bảng sử dụng jquery này:

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

Kết quả trông giống như sau:

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 
    ) 

) 

Mảng này được sử dụng để cập nhật_batch trong bảng của tôi.

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 được gọi bộ điều khiển có hành động này:

$.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]); 
} 

Đây là mô hình .

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"; 
    } 
} 

Vấn đề lớn của tôi là, tôi muốn sử dụng lô cập nhật. Nhưng, tôi chỉ muốn cập nhật trường manhour và ac. Tôi thực sự bị mắc kẹt trong nhiều ngày, bất kỳ sự trợ giúp nào được đánh giá cao như vậy

CẬP NHẬT Cảm ơn rất nhiều vì Mr.Sultan. Bây giờ, mã của tôi là nhìn như thế này:

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

Mô hình của tôi nhận được một số rắc rối, coz, tôi cần ba thông số để 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"; 
    } 
} 

Cảm ơn cho các giải pháp

+0

Điều này có liên quan gì đến [tag: batch-file]? – aschipfl

Trả lời

0

Trong điều khiển của bạn, thay thế chức năng của bạn bằng chức năng này:

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

Có s phương pháp luôn để đối phó với một vấn đề, nhưng chúng ta phải chọn phương pháp thông minh nhất.

Bạn chỉ cần chọn các phần tử bắt buộc hơn là đẩy mảng và đăng chúng. Vui lòng cung cấp thuộc tính tên cho các yếu tố đầu vào và trong chế độ xem (html) và trong kiểm tra địa điểm jQuery để chọn các yếu tố bắt buộc của bạn. Không thay đổi bất cứ điều gì trong mô hình và bộ điều khiển.

BướC# 1 Phần tử nhập phải có thuộc tính tên.

<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">

BướC# 2 jQuery - Trường hợp bạn nhận được giá trị vị trí sau mã thay vì row_data.push($(this).val()); này.

if($(this).attr("name")==='name_type' || $(this).attr("name")==='MANHOUR' || $(this).attr("name")==='A/C'){ 
row_data.push($(this).val());} 
Các vấn đề liên quan