2

I am getting Id as Array when doing batch update. Not able to update data to my existing data table . Please help me

My Error Message is shown below Error Message as Image Format

Controller

public function update() 
{

$sID   = $this->input->post('test_id[]');
$sAmt  = $this->input->post('test_priceUpdt[]');
$sOfficeId = $this->input->post('fran_office_id');
$edited_test = array();
for ($i=0; $i < sizeof($sID); $i++)
  {
   if(!empty($sID[$i])) 
   $edited_test[$i] = array(
    'test_id' => $sID[$i],
    'test_priceUpdt' => $sAmt[$i],
    'fran_office_id' => $sOfficeId
   );
}

$edited_test = json_encode($edited_test); 
$data['franchise_tst_pkg'] = (object)$postData = array (
    array(
     'test_id' => $sID,
     't_franchise_price' => $edited_test
    )
   
 ); 
 if ($this->form_validation->run() === true) {
 $this->franchise_price_model->batchTestupdate($postData))
 }

}

View

<tr>
  <td>
    <input type="text" name="test_name[]" class="form-control" placeholder="" value="<?php echo $subject->test_name; ?>" disabled>
    <input type="hidden" name="test_id[]" class="form-control" placeholder="" value="<?php echo $subject->test_id; ?>">
  </td>
  <td>
    <input type="text" name="test_price[]" class="form-control" placeholder="" value="<?php echo $subject->test_price; ?>" disabled>
  </td>
  <td>
    <input type="text" name="test_priceUpdt[]" class="form-control" placeholder="" value="<?php echo $subject->test_price; ?>" id="test_priceEdt">
  </td>
</tr>

Modal

public function batchTestupdate($data = []) 
{ $this->db
        ->update_batch($this->table_test, $data , 'test_id');
}
Pradeep
  • 9,667
  • 13
  • 27
  • 34
Biswajit17
  • 41
  • 11

2 Answers2

1

The second parameter in your update_batch(..) function is structured wrongly.

Change:

$data['franchise_tst_pkg'] = (object)$postData = array (
                array(
                    'test_id' => $sID,
                    't_franchise_price' => $edited_test
                )

To:

$postData = array();
if(is_array($sID)){
    foreach($sID as $k=>$v){
        $postData[] = array(
            'test_id' => $v,
            't_franchise_price' => $edited_test
        );
    }
}
$data['franchise_tst_pkg'] = (object)$postData;
Karlo Kokkak
  • 3,674
  • 4
  • 18
  • 33
  • Sir I am getting error message like this , after changed the code as your suggest -- **A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Filename: branch/Franchise_price.php Line Number: 97** – Biswajit17 May 29 '18 at 06:21
  • Updated my answer. – Karlo Kokkak May 29 '18 at 07:24
  • 1
    Sir , I done with this code and it working - if(!empty($sID)){ foreach($sID as $k=>$v ){ $postData[] = array( 'test_id' => $v, 't_franchise_price' => $edited_test ); } $data['franchise_tst_pkg'] = (object)$postData; } – Biswajit17 May 29 '18 at 09:22
  • Thank u sir for your kind support – Biswajit17 May 29 '18 at 09:22
  • Sir, 1 problem is occur. As for **batch update** we are expecting for one row one value will insert to the field but , here all values is updated to a single filed.For example, I have one row Id-17 , expecting only -- **{"test_id":"17","test_priceUpdt":"1221","fran_office_id":"12"}** for row id 17 . But receiving like this to one column(named-**t_franchise_price**) of the row ----- (shown in next comment ) – Biswajit17 May 29 '18 at 10:05
  • [{"test_id":"24","test_priceUpdt":"200","fran_office_id":"12"},{"test_id":"22","test_priceUpdt":"300","fran_office_id":"12"},{"test_id":"23","test_priceUpdt":"200","fran_office_id":"12"},{"test_id":"21","test_priceUpdt":"200","fran_office_id":"12"},{"test_id":"25","test_priceUpdt":"2000","fran_office_id":"12"},{"test_id":"17","test_priceUpdt":"1221","fran_office_id":"12"},{"test_id":"19","test_priceUpdt":"10","fran_office_id":"12"}] **what should i do ?** – Biswajit17 May 29 '18 at 10:05
  • @ Karlo Kokkak , Sir - on this update_batch is it possible to without replacing the previous data next updated value will puts by comma separated as an array form on the SAME FILED . For example - **[{"test_id":"24","test_priceUpdt":"200","fran_office_id":"12"},{"test_id":"24","test_priceUpdt":"250","fran_office_id":"12"}]** - Like this , 1st updated value will stay and also 2nd updated value keep there with only Updating the **"test_priceUpdt":"200" to "test_priceUpdt":"250"** . I am not sure, but is it possible ? please suggest me . Thanks. – Biswajit17 May 30 '18 at 08:35
  • Sir for your information.My one problem share with my previous comment box. – Biswajit17 May 30 '18 at 09:10
  • Sir for your information.My one problem share with my previous comment box. – Biswajit17 May 30 '18 at 12:12
  • What is the maximum records are you updating at once? – Karlo Kokkak May 30 '18 at 12:18
  • Sir , right now I have 10 no of rows and there I am updating 10no of rows with **{"test_id":"x","test_priceUpdt":"x","fran_office_id":"x"}** – Biswajit17 May 30 '18 at 12:33
  • And in future may be rows will be increased . **Sir here I upload my Problem in Details ,please come to this link** https://stackoverflow.com/questions/50601471/codeigniter-update-batch-without-replacing-the-previous-data-next-updated-value – Biswajit17 May 30 '18 at 12:34
0

You can change model as:

public function batchTestupdate($data = []) 
{ $this->db
        ->update_batch($this->table_test, explode(',', $data) , 'test_id');
}
  • Not run the code . getting error like --- **A Database Error Occurred You must use the "set" method to update an entry. Filename: models/branch/Franchise_price_model.php** – Biswajit17 May 29 '18 at 05:40
  • Try using var_dump($data) before the query. May be $data is coming insider array in array form – Sambhaji Katrajkar May 29 '18 at 05:42