Biswajit17
Biswajit17

Reputation: 41

Getting id as Array in update_batch in codeigniter

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

Upvotes: 2

Views: 474

Answers (2)

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

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;

Upvotes: 1

Sambhaji Katrajkar
Sambhaji Katrajkar

Reputation: 407

You can change model as:

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

Upvotes: 0

Related Questions