You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CodeIgniter控制器中实现总分0的学生成绩不入库?

实现录入学生成绩时过滤总分0的数据不插入数据库

需求:在录入学生成绩时,总分(get_ca1+get_ca2+get_ca3+get_ca4+get_exam)为0的学生数据完全不插入数据库。

原控制器代码

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

修改方案

原代码的问题在于:循环结束后才判断总分,且只校验了最后一个学生的成绩,同时所有学生数据都被加入了插入数组。正确的做法是在循环内逐个判断学生总分,过滤掉总分0的数据。

修改后的代码

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 = isset($this->input->post('exam_group_student_attendance_' . $exam_group_student_value)) 
                    ? $this->input->post('exam_group_student_attendance_' . $exam_group_student_value) 
                    : "present";

                // 获取当前学生各项成绩并转为整数
                $ca1 = intval($this->input->post('exam_group_student_ca1_' . $exam_group_student_value));
                $ca2 = intval($this->input->post('exam_group_student_ca2_' . $exam_group_student_value));
                $ca3 = intval($this->input->post('exam_group_student_ca3_' . $exam_group_student_value));
                $ca4 = intval($this->input->post('exam_group_student_ca4_' . $exam_group_student_value));
                $exam = intval($this->input->post('exam_group_student_exam_' . $exam_group_student_value));
                
                // 计算总分
                $totalScore = $ca1 + $ca2 + $ca3 + $ca4 + $exam;

                // 仅总分大于0时,将数据加入插入数组
                if ($totalScore > 0) {
                    $studentData = 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' => $ca1,
                        'get_ca2' => $ca2,
                        'get_ca3' => $ca3,
                        'get_ca4' => $ca4,
                        'get_exam' => $exam,
                        'note' => $this->input->post('exam_group_student_note_' . $exam_group_student_value),
                    );
                    $insert_array[] = $studentData;
                }
            }
        }

        // 插入数组不为空时才执行批量插入
        if (!empty($insert_array)) {
            $this->examgroupstudent_model->add_result($insert_array);
        }

    }
    $array = array('status' => '1', 'error' => '', 'message' => $this->lang->line('success_message'));
    echo json_encode($array);
}

关键修改说明

  1. 循环内逐个校验:对每个学生单独计算总分,从源头过滤掉总分0的数据,避免无效数据进入插入数组。
  2. 类型转换:将成绩转为整数,防止字符串拼接导致的计算错误。
  3. 空数组判断:执行批量插入前先检查数组是否为空,避免无意义的数据库操作。
  4. 代码简化:用三元运算符简化出勤状态的赋值逻辑,提升代码可读性。

内容的提问来源于stack exchange,提问作者Esepebbles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 18:54:19