Batch updating in Codeigniter

Viewed 51

I'm trying to batch update two records..

'studentextendID' is the primary key, so it's dynamic.. 'studentID' is static, What im trying to do is to update two records of the same 'studentID'

But instead, the output produces 6 arrays, the same exact number in a single array..

When it should produce 2 arrays only. and i put different values for each record, but one of these values goes to the two records instead.

Any advices?

My Controller:

$studentextends = $this->studentextend_m->get_order_by_studentextend(array('studentID' => $studentID));
                        $studentExtendArray = [];
                        if(customCompute($studentextends)) {

                            foreach($studentextends as $studentextend) {
                                $studentextendID = $studentextend->studentextendID;
                                $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');
                        }

Output:

Array(
[0] => Array
    (
        [studentextendID] => 1131
        [studentID] => 306
        [subject] => subject1
        [subjectng] => A513
        [subjectlg] => 113
        [subjectcre] => 36513
    )

[1] => Array
    (
        [studentextendID] => 1131
        [studentID] => 306
        [subject] => subject2
        [subjectng] => A514
        [subjectlg] => 114
        [subjectcre] => 36514
    )

[2] => Array
    (
        [studentextendID] => 1131
        [studentID] => 306
        [subject] => 
        [subjectng] => 
        [subjectlg] => 
        [subjectcre] => 
    )

[3] => Array
    (
        [studentextendID] => 1132
        [studentID] => 306
        [subject] => subject1
        [subjectng] => A513
        [subjectlg] => 113
        [subjectcre] => 36513
    )

[4] => Array
    (
        [studentextendID] => 1132
        [studentID] => 306
        [subject] => subject2
        [subjectng] => A514
        [subjectlg] => 114
        [subjectcre] => 36514
    )

[5] => Array
    (
        [studentextendID] => 1132
        [studentID] => 306
        [subject] => 
        [subjectng] => 
        [subjectlg] => 
        [subjectcre] => 
    ) )

Expected Output:

Array (
[0] => Array
    (
        [studentextendID] => 1131
        [studentID] => 306
        [subject] => subject1
        [subjectng] => A513
        [subjectlg] => 113
        [subjectcre] => 36513
    )

[1] => Array
    (
        [studentextendID] => 1132
        [studentID] => 306
        [subject] => subject2
        [subjectng] => A514
        [subjectlg] => 114
        [subjectcre] => 36514
    )
0 Answers
Related