我遇到一些逻辑错误的问题

时间:2019-09-28 09:53:44

标签: php codeigniter codeigniter-3

我想对有一定条件的单个学生人数求和。条件是,如果一名学生未通过任何一门课程,那么它将给我0值而不是他的任何数字的总和。使用此条件会给我错误的价值。现在我遇到了这些问题,我尝试了很多事情,但是没有用。在下面,我为您提供代码和输出以及所需的输出。

控制器:

if (!empty($examSchedule)) {
    $new_array = array();
    foreach ($studentList as $stu_key => $stu_value) {
        $array = array();
        $array['student_id'] = $stu_value['id'];
        $array['admission_no'] = $stu_value['admission_no'];
        $array['roll_no'] = $stu_value['roll_no'];
        $array['firstname'] = $stu_value['firstname'];
        $array['lastname'] = $stu_value['lastname'];
        $array['dob'] = $stu_value['dob'];
        $array['father_name'] = $stu_value['father_name'];
        $x = array();
        foreach ($examSchedule as $ex_key => $ex_value) {
            $exam_array = array();
            $exam_array['exam_schedule_id'] = $ex_value['id'];
            $exam_array['exam_id'] = $ex_value['exam_id'];
            $exam_array['subject_id'] = $ex_value['subject_id'];
            $exam_array['full_marks'] = $ex_value['full_marks'];
            $exam_array['passing_marks'] = $ex_value['passing_marks'];
            $exam_array['exam_name'] = $ex_value['name'];
            $exam_array['exam_type'] = $ex_value['type'];
            $student_exam_result = $this->examresult_model->get_exam_result($ex_value['id'], $stu_value['id']);
            $exam_array['attendence'] = $student_exam_result->attendence;
            $exam_array['get_marks'] = $student_exam_result->get_marks;
            $x[] = $exam_array;
        }
        $array['exam_array'] = $x;
        $new_array[] = $array;
    }
    $data['examSchedule'] = $new_array;
}

if ($this->input->post('save_exam') == "save_exam") {
    $ex_array = array();
    $exam_id = $this->input->post('exam_id');
    $student_array = $this->input->post('student');
    $exam_array = $this->input->post('exam_schedule');
    $total_marks = array();
    foreach ($student_array as $key => $student) {
        foreach ($examSchedule as $ex_key => $ex_value) {
        foreach ($exam_array as $key => $exam) {
            $record['get_marks'] = 0;
            $record['attendence'] = "pre";
            if ($this->input->post('student_absent' . $student . "_" . $exam) == "") {
                $get_marks = $this->input->post('student_number' . $student . "_" . $exam);
                $record['get_marks'] = $get_marks;

                $passing_marks = $ex_value['passing_marks'];
                if ($get_marks != '0' && $get_marks>=$passing_marks) {
                    $total_marks[$student] += $get_marks; 

                } else {
                     $total_marks[$student] = 0;
                     break;    
                }


            } else {
                $record['attendence'] = $this->input->post('student_absent' . $student . "_" . $exam);
                                        }
            $record['exam_schedule_id'] = $exam;
            $record['student_id'] = $student;
            $record['exam_id'] = $exam_id;


            $inserted_id = $this->examresult_model->add_exam_result($record);

            if ($inserted_id) {
                $ex_array[$student] = $exam_id;
            }

        }


        $total['total_mark'] = $total_marks[$student];                           
        $total['exam_id'] = $this->input->post('exam_id');
        $total['student_id'] = $student;
        $total['class_id'] = $class_id;
        $total['section_id'] = $section_id;
        $total['year'] = date("Y");

        $total_mark = $this->examresult_model->add_total_result($total);

    }
    echo "<pre>";
        print_r( $total['total_mark']);
}    
exit();

    if (!empty($ex_array)) {
        $this->mailsmsconf->mailsms('exam_result', $ex_array, NULL, $exam_array);
    }
    redirect('admin/mark');
}

输出(我得到):

3213
3171
1248
2989
4291
0
0
0
0
0
0
0
0

输出(我想要):

459
453
416
0 //(427 is the sum but one subject fail thatswhy it sum zero)
613
0
0
0
0
0
0
0
0

student_array的值(这些都是学生ID):

Array(
    [0] => 110
    [1] => 111
    [2] => 120
    [3] => 121
    [4] => 112
    [5] => 113
    [6] => 114
    [7] => 122
    [8] => 115
    [9] => 116
    [10] => 117
    [11] => 118
    [12] => 119
)

exam_array的值:

Array
(
    [0] => 84
    [1] => 85
    [2] => 86
    [3] => 87
    [4] => 88
    [5] => 89
    [6] => 90
)

examSchedule的值:

Array
(
    [0] => Array
        (
            [student_id] => 110
            [admission_no] => 01
            [roll_no] => 01
            [firstname] => Md. Abdur
            [lastname] => Rahaman
            [dob] => 2015-05-12
            [father_name] => Sowpan Chow
            [exam_array] => Array
                (
                    [0] => Array
                        (
                            [exam_schedule_id] => 84
                            [exam_id] => 7
                            [subject_id] => 4
                            [full_marks] => 100
                            [passing_marks] => 33
                            [exam_name] => Bangla
                            [exam_type] => Theory
                            [attendence] => pre
                            [get_marks] => 75.00
                        )

                    [1] => Array
                        (
                            [exam_schedule_id] => 85
                            [exam_id] => 7
                            [subject_id] => 1
                            [full_marks] => 100
                            [passing_marks] => 33
                            [exam_name] => English
                            [exam_type] => Theory
                            [attendence] => pre
                            [get_marks] => 82.00
                        )

                    [2] => Array
                        (
                            [exam_schedule_id] => 86
                            [exam_id] => 7
                            [subject_id] => 2
                            [full_marks] => 100
                            [passing_marks] => 33
                            [exam_name] => Math
                            [exam_type] => Theory
                            [attendence] => pre
                            [get_marks] => 88.00
                        )

                    [3] => Array
                        (
                            [exam_schedule_id] => 87
                            [exam_id] => 7
                            [subject_id] => 3
                            [full_marks] => 100
                            [passing_marks] => 33
                            [exam_name] => Religion
                            [exam_type] => Theory
                            [attendence] => pre
                            [get_marks] => 87.00
                        )

                    [4] => Array
                        (
                            [exam_schedule_id] => 88
                            [exam_id] => 7
                            [subject_id] => 10
                            [full_marks] => 50
                            [passing_marks] => 16
                            [exam_name] => Art & Craft
                            [exam_type] => Theory
                            [attendence] => pre
                            [get_marks] => 45.00
                        )

                    [5] => Array
                        (
                            [exam_schedule_id] => 89
                            [exam_id] => 7
                            [subject_id] => 9
                            [full_marks] => 50
                            [passing_marks] => 16
                            [exam_name] => General Knowledge
                            [exam_type] => Theory
                            [attendence] => pre
                            [get_marks] => 42.00
                        )

                    [6] => Array
                        (
                            [exam_schedule_id] => 90
                            [exam_id] => 7
                            [subject_id] => 11
                            [full_marks] => 50
                            [passing_marks] => 16
                            [exam_name] => Computer
                            [exam_type] => Theory
                            [attendence] => pre
                            [get_marks] => 40.00
                        )

                )

        )
 [1] => Array
        (
           #value
        )
[2] => Array
        (
           #value
        )
[3] => Array
        (
           #value
        )
[4] => Array
        (
           #value
        )
[5] => Array
        (
           #value
        )..
[12] => Array
        (
           #value
        )

1 个答案:

答案 0 :(得分:0)

您可以进行单独的foreach循环以获取单个学生的全部成绩,然后在进行通过分数验证/添加之前检查其是否包含任何0成绩:

if ($this->input->post('save_exam') == "save_exam") {
    $ex_array = array();
    $exam_id = $this->input->post('exam_id');
    $student_array = $this->input->post('student');
    $exam_array = $this->input->post('exam_schedule');
    $total_marks = array();
    foreach ($student_array as $key => $student) {
        // check if current student having 0 mark
        $student_mark = [];
        foreach ($exam_array as $key => $exam) {
            $student_mark[$student][] = $this->input->post('student_number' . $student . "_" . $exam);
        }
        $zero_mark_flag = in_array(0, $student_mark[$student], true) ? true : false; // return true if contains 0 mark
        foreach ($examSchedule as $ex_key => $ex_value) {
            foreach ($exam_array as $key => $exam) {
                $record['get_marks'] = 0;
                $record['attendence'] = "pre";
                if ($this->input->post('student_absent' . $student . "_" . $exam) == "") {
                    $get_marks = $this->input->post('student_number' . $student . "_" . $exam);
                    $record['get_marks'] = $get_marks;

                    $passing_marks = $ex_value['passing_marks'];
                    // there are 0 on one or more student marks, so skip the mark addition
                    if ($zero_mark_flag === true) {
                        $total_marks[$student] = 0;
                        break;    

                    } else if ($get_marks != '0' && $get_marks>=$passing_marks) {
                        $total_marks[$student] += $get_marks; 
                    }


                } else {
                    $record['attendence'] = $this->input->post('student_absent' . $student . "_" . $exam);
                                            }
                $record['exam_schedule_id'] = $exam;
                $record['student_id'] = $student;
                $record['exam_id'] = $exam_id;


                $inserted_id = $this->examresult_model->add_exam_result($record);

                if ($inserted_id) {
                    $ex_array[$student] = $exam_id;
                }

            }


            $total['total_mark'] = $total_marks[$student];                           
            $total['exam_id'] = $this->input->post('exam_id');
            $total['student_id'] = $student;
            $total['class_id'] = $class_id;
            $total['section_id'] = $section_id;
            $total['year'] = date("Y");

            $total_mark = $this->examresult_model->add_total_result($total);

        }
        echo "<pre>";
        print_r( $total['total_mark']);
    }    
    exit();

    if (!empty($ex_array)) {
        $this->mailsmsconf->mailsms('exam_result', $ex_array, NULL, $exam_array);
    }
    redirect('admin/mark');
}

因此,学生分数将始终设置为0。