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

0

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

12 Comments

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
Updated my answer.
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; }
Thank u sir for your kind support
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 )
|
0

You can change model as:

public function batchTestupdate($data = []) 
{ $this->db
        ->update_batch($this->table_test, explode(',', $data) , 'test_id');
}

2 Comments

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
Try using var_dump($data) before the query. May be $data is coming insider array in array form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.