PHP Codeigniter batch update in some places

I have a table in html like this: VIEW

<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 I got all the value in this table using this 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); }); 

The result is as follows:

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

This array is used for update_batch in my table.

 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 to receive a controller call, do the following:

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

This is a model .

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

My big problem is that I want to use the service pack. But I just want to just update manhour and ac field. I really got stuck for a few days, any help she appreciated so much

UPDATE Many thanks to Mr. Sultan. Now my code looks like this:

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

My model gets some problems because I need three update_batch parameters

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

Thanks for the decision

+6
source share
2 answers

There are several ways to deal with the problem, but we should choose only the smartest method.

You need to select only the required elements, not insert an array and publish them. Specify attribute attributes for input elements both in the (html) view and in the jQuery validation window to select the desired elements. Do not change anything in the model and controller.

Step # 1 The input element must have a name attribute.

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

Step # 2 JQUERY - where you get the values ​​instead of the row_data.push($(this).val()); code row_data.push($(this).val()); instead of code 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());} 
+1
source

In your controller, replace your function with the following:

 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]); } 
0
source

All Articles