Home > front end >  Batch Update with Dynamic IDs Codeigniter
Batch Update with Dynamic IDs Codeigniter

Time:06-24

I'm trying to batch update a table by an ID

My controller:

if(customCompute($this->data['student'])) {
                            $studentextendID = $this->data['student']->studentextendID;
                            $this->data['students'] = $this->studentextend_m->get_studentextend(array('studentextendID' => $studentextendID));
                            } else {
                                $this->data['students'] = [];
                        }
        

                        $post = $this->input->post();
                        for($i=0; $i < count($post['subject']); $i  ) {
                            $studentExtendArray[] = array(
                                'studentextendID' => $studentextendID,
                                'studentID' => $studentID,
                                'subject' => $post['subject'][$i],
                                'subjectng' => $post['subjectng'][$i],
                                'subjectlg' => $post['subjectlg'][$i],
                                'subjectcre' => $post['subjectcre'][$i],
    
    
                            );
                        $this->db->update_batch('studentextend', $studentExtendArray, 'studentextendID');

                        }

My Model

 function get_studentextend($array=NULL, $signal=FALSE) {
    $query = parent::get($array, $signal);
    return $query;
}

Array Output:

Array (
[0] => Array
    (
        [studentextendID] => 143
        [studentID] => 97
        [subject] => 
        [subjectng] => 5235
        [subjectlg] => 5231
        [subjectcre] => 523155
    )

[1] => Array
    (
        [studentextendID] => 143
        [studentID] => 97
        [subject] => 
        [subjectng] => 2
        [subjectlg] => 99
        [subjectcre] => 3
    ) )

As you can see, 'studentextendID' is duplicated on both arrays, when it should be dynamically obtained, for example: 143 and 144, because there are two rows in the table with the same 'studentID'

CodePudding user response:

You can try with change in model

public function update_batchs($table, $data)
{
    if ($data) {
        return $this->db->update_batch($table, $data, 'studentextendID');
    }
}

CodePudding user response:

You are not pass the $studentextendID in for loop.You need to pass this in for loop.

$studentextendID = $this->data['student']->studentextendID;

And also please verify $this->data['student'].

  • Related