Coder Codes
Coder Codes

Reputation: 129

Is there a way to condition inside an array using update batch? CodeIgniter PHP

I am trying to update grades of student using update batch (multiple update)

However the problem arises when conditioning in the model, whereas the array condition does not accept else if statement

in this line, 'equiv' => $data['grade'][$i] == 65 ? 5 : 0, is there a way to make it a full condition statement where I can use else if and remove the else or :0 at the end of the line. The :0 makes all other data 0 which those data should not be affected.

public function updateAll(){
    $id = $this->input->post('studentID');

    $data = $this->input->post();
    
        for($i = 0; $i < (count($data['studentID'])); $i++){
        
            $batch[] = array(
            'studentID' => $data['studentID'][$i],
            'grade' => $data['grade'][$i],
            'equiv' => $data['grade'][$i] == 65 ? 5 : 0
            );

        }

    return $this->db->update_batch('tbl_college_grades', $batch, 'studentID');
}

Upvotes: 0

Views: 34

Answers (1)

Mhmmad Rehan
Mhmmad Rehan

Reputation: 111

THIS IS WORK FOR ME, GOOD LUCK

public function updateAll(){
    $id = $this->input->post('studentID');

    $data = $this->input->post();
    $selected_equiv;
    
        for($i = 0; $i < (count($data['studentID'])); $i++){
            data['grade'][$i] == 65 ? $selected_equiv = 5 : 0
            $batch[] = array(
            'studentID' => $data['studentID'][$i],
            'grade' => $data['grade'][$i],
            'equiv' => $selected_equiv
            );

        }

    return $this->db->update_batch('tbl_college_grades', $batch, 'studentID');
}

Upvotes: 2

Related Questions