CodeIgniter - 多维数组问题

时间:2014-07-09 16:19:52

标签: php arrays codeigniter multidimensional-array undefined

我的系统在控制器中的多维数组方面存在很大问题。所以这就是问题所在。

CONTROLLER

function save_exam(){
            if($_POST){
                $this->load->model('teacher_model');

                $question = $_POST['question'];
                $choice = $_POST['choice'];
                $title = $_POST['title'];
                $subject_id = $_POST['subject_id'];
                $password = $_POST['password'];
                $items = count($question);

                $exam = array(
                        'subject_id'=>$subject_id,
                        'title'=>$title,
                        'password'=>$password
                    );

                $exam_id = $this->teacher_model->save_exam($exam);

                for($x = 0; $x < $items; $x++){
                    $each_question = $question[$x]['question']; 
                    $each_answer = $question[$x]['answer'];
                    $choice_a = $choice[$x]['a'];
                    $choice_b = $choice[$x]['b'];
                    $choice_c = $choice[$x]['c'];
                    $choice_d = $choice[$x]['d'];

                    $question = array(
                            'exam_id'=>$exam_id,
                            'description'=>$each_question
                        );

                    $question_id = $this->teacher_model->save_question($question);


                    $answer = array(
                            'exam_id'=>$exam_id,
                            'question_id'=>$question_id,
                            'choice_a'=>$choice_a,
                            'choice_b'=>$choice_b,
                            'choice_c'=>$choice_c,
                            'choice_d'=>$choice_d,
                            'answer'=>$each_answer
                        );

                    $this->teacher_model->save_answer($answer);
                }

            }
        }

VIEW(指向控制器的视图)

<h2>Create Exams</h2>

<form method="post" action="save_exam">
    <?php
        echo '<input type="hidden" name="subject_id" value="' . $subject_id . '" >';
        echo '<input type="hidden" name="title" value="' . $title . '" >';
        echo '<input type="hidden" name="password" value="' . $password . '" >';
        $items = $number_items;
        for($x=0; $x<$items; $x++){
            $id = $x;
            echo $id + 1 . '.) ';
            echo '<input type="text" name="question[' . $id . '][question]" required></br>';
            echo 'a. <input type="text" name="choice[' . $id . '][a]" required></br>';
            echo 'b. <input type="text" name="choice[' . $id . '][b]" required></br>';
            echo 'c. <input type="text" name="choice[' . $id . '][c]" required></br>';
            echo 'd. <input type="text" name="choice[' . $id . '][d]" required></br>';
            echo 'answer : ';
            echo 'a - <input type="radio" name="question[' . $id . '][answer]" value="a" required> | ';
            echo 'b - <input type="radio" name="question[' . $id . '][answer]" value="b" required> | ';
            echo 'c - <input type="radio" name="question[' . $id . '][answer]" value="c" required> | ';
            echo 'd - <input type="radio" name="question[' . $id . '][answer]" value="d" required> | ';
            echo '</br></br>';
        }
    ?>
    <input type="submit" value="submit exam">
</form>

MODEL

        function save_exam($exam){
            $this->db->insert('exam', $exam);
            return $this->db->insert_id();
        }

        function save_question($question){
            $this->db->insert('question', $question);
            return $this->db->insert_id();
        }

        function save_answer($answer){
            $this->db->insert('answer', $answer);
        }

问题是,每当我提交时,我都会遇到此问题。

______________________________________________________________________
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: controllers/teacher.php

Line Number: 43
______________________________________________________________________

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: controllers/teacher.php

Line Number: 44
______________________________________________________________________

A Database Error Occurred

Error Number: 1048

Column 'description' cannot be null

INSERT INTO `question` (`exam_id`, `description`) VALUES (3, NULL)

Filename: F:\www\sites\capstone_system\system\database\DB_driver.php

Line Number: 330

希望你们能帮助我,并提前感谢你们!

巫女..

0 个答案:

没有答案