When adding scores for students, I would like those with a total score of 0 not to be inserted into the database at all.
Controller:
public function entrymarks()
{
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('exam_group_class_batch_exam_subject_id', 'Subject', 'required|trim|xss_clean');
if ($this->form_validation->run() == false) {
$data = array(
'exam_group_class_batch_exam_subject_id' => form_error('exam_group_class_batch_exam_subject_id'),
);
$array = array('status' => 0, 'error' => $data);
echo json_encode($array);
} else {
$exam_group_student_id = $this->input->post('exam_group_student_id');
$insert_array = array();
$update_array = array();
if (!empty($exam_group_student_id)) {
foreach ($exam_group_student_id as $exam_group_student_key => $exam_group_student_value) {
$attendance_post = $this->input->post('exam_group_student_attendance_' . $exam_group_student_value);
if (isset($attendance_post)) {
$attendance = $this->input->post('exam_group_student_attendance_' . $exam_group_student_value);
} else {
$attendance = "present";
}
$array = array(
'exam_group_class_batch_exam_subject_id' => $this->input->post('exam_group_class_batch_exam_subject_id'),
'exam_group_class_batch_exam_student_id' => $exam_group_student_value,
'attendence' => $attendance,
'get_ca1' => $this->input->post('exam_group_student_ca1_' . $exam_group_student_value),
'get_ca2' => $this->input->post('exam_group_student_ca2_' . $exam_group_student_value),
'get_ca3' => $this->input->post('exam_group_student_ca3_' . $exam_group_student_value),
'get_ca4' => $this->input->post('exam_group_student_ca4_' . $exam_group_student_value),
'get_exam' => $this->input->post('exam_group_student_exam_' . $exam_group_student_value),
'note' => $this->input->post('exam_group_student_note_' . $exam_group_student_value),
);
$insert_array[] = $array;
}
}
if ( intval($array['get_ca1'] $array['get_ca2'] $array['get_ca3'] $array['get_ca4'] $array['get_exam'] ) > 0 ) {
$this->examgroupstudent_model->add_result($insert_array);
}
}
$array = array('status' => '1', 'error' => '', 'message' => $this->lang->line('success_message'));
echo json_encode($array);
}
}
I will like to first get the sum total of get_ca1
get_ca2
get_ca3
get_ca4
get_exam
then if it is 0, don't insert.
Please how do I do this?
CodePudding user response:
I don't think you can access the array value on the fly without any workaround, and you've placed the insert outside the foreach
.
It would be best if you spent more time on these.
- Code Indexing
- Use proper IDE. Not like notepad.
- Debug all and every line for errors.
Do like this.
Since you're not using batch insert, you can do it one by one.
$cal1 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$cal2 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$cal3 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$cal4 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$exam = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$total = $cal1 $cal2 $cal3 $cal4 $exam;
$array = array(
'exam_group_class_batch_exam_subject_id' => $this->input->post('exam_group_class_batch_exam_subject_id'),
'exam_group_class_batch_exam_student_id' => $exam_group_student_value,
'attendence' => $attendance,
'get_ca1' => $cal1,
'get_ca2' => $cal2,
'get_ca3' => $cal3,
'get_ca4' => $cal4,
'get_exam' => $exam,
'note' => $this->input->post('exam_group_student_note_' . $exam_group_student_value)
);
if ($total > 0) {
$this->examgroupstudent_model->add_result($array);
}
So final code will be
public function entrymarks()
{
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('exam_group_class_batch_exam_subject_id', 'Subject', 'required|trim|xss_clean');
if ($this->form_validation->run() == false) {
$data = array(
'exam_group_class_batch_exam_subject_id' => form_error('exam_group_class_batch_exam_subject_id'),
);
$array = array('status' => 0, 'error' => $data);
echo json_encode($array);
} else {
$exam_group_student_id = $this->input->post('exam_group_student_id');
$insert_array = array();
$update_array = array();
if (!empty($exam_group_student_id)) {
foreach ($exam_group_student_id as $exam_group_student_key => $exam_group_student_value) {
$attendance_post = $this->input->post('exam_group_student_attendance_' . $exam_group_student_value);
if (isset($attendance_post)) {
$attendance = $this->input->post('exam_group_student_attendance_' . $exam_group_student_value);
} else {
$attendance = "present";
}
$cal1 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$cal2 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$cal3 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$cal4 = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$exam = $this->input->post('exam_group_student_ca1_' . $exam_group_student_value);
$total = $cal1 $cal2 $cal3 $cal4 $exam;
$array = array(
'exam_group_class_batch_exam_subject_id' => $this->input->post('exam_group_class_batch_exam_subject_id'),
'exam_group_class_batch_exam_student_id' => $exam_group_student_value,
'attendence' => $attendance,
'get_ca1' => $cal1,
'get_ca2' => $cal2,
'get_ca3' => $cal3,
'get_ca4' => $cal4,
'get_exam' => $exam,
'note' => $this->input->post('exam_group_student_note_' . $exam_group_student_value)
);
if ($total > 0) {
$this->examgroupstudent_model->add_result($array);
}
}
}
}
$array = array('status' => '1', 'error' => '', 'message' => $this->lang->line('success_message'));
echo json_encode($array);
}