od jakiegoś czasu robię projekt egzaminatora online, quizu. Wszystko szło dobrze, dopóki nie chciałem zapisać wybranych przez użytkownika odpowiedzi do bazy danych. Sam egzamin wyświetlam w formie formularza, pobieram ilość pytań a następnie wyświetlam je jako radiobuttony w formularzu. Problemem jest wychwycenie wybranych odpowiedzi i zapisanie je w bazie. Męczę się już trochę z tym i już mi brakuje pomysłów, wiem, że problem jest z moimi zmiennymi, prawdopodobnie źle je przekazuję i źle próbuje odczytać. Oto mój kod:
Kontroler
public function begin_test($id_test){ $this->form_validation->set_rules('username', 'Username', 'min_length[1]|max_length[30]'); $i=0; $data['max'] = $this->Students_model->amount_of_questions($id_test); $data['questions_test'] = $this->Students_model->get_questions_for_exam($id_test); $data['test_name'] = $this->Students_model->get_test_name_by_id($id_test); $data['subject_name'] = $this->Workers_model->get_subject_name_by_id_test($id_test); if($this->form_validation->run() == FALSE){ $data['main_content'] = 'students/exam_view'; $this->load->view('layouts/main',$data); } else { $this->Students_model->add_result_to_database($id_test,$results); $this->session->set_flashdata('test_finished','Test został wysłany. Wyniki będą dostępne w sekcji Moje Testy'); redirect('students_site/show_students_tests'); } } }
Widok
Pozostały czas Y <br> <link rel="stylesheet" href="assets/css/bootstrap.min.css"> <?php $i=1; ?> <?php foreach($questions_test as $question_test) : ?> <?php 'name' => 'answer'.$question_test->id_test_item, 'id' => $question_test->id_test_item, 'value' => $question_test->answerA, 'checked' => false, 'style' => 'margin:10px', ); ?> <?php 'name' => 'answer'.$question_test->id_test_item, 'id' => $question_test->id_test_item, 'value' => $question_test->answerB, 'checked' => false, 'style' => 'margin:10px', ); ?> <?php 'name' => 'answer'.$question_test->id_test_item, 'id' => $question_test->id_test_item, 'value' => $question_test->answerC, 'checked' => false, 'style' => 'margin:10px', ); ?> <?php 'name' => 'answer'.$question_test->id_test_item, 'id' => $question_test->id_test_item, 'value' => $question_test->answerD, 'checked' => false, 'style' => 'margin:10px', ); ?> <?php $i=$i+1; ?> <?php endforeach; ?> <br><br> <?php "name" => "submit", "class" => "btn btn-primary"); ?> <p> </p>
Model:
public function add_result_to_database($id_test,$quess){ foreach ($quess as $ques){ if($this->input->post('answer'.$ques->id)->checked == true){ 'id_question' => $this->input->post('answer'.$ques->id), 'id_student_answer' => $this->input->post('answer').$ques->value, 'id_user' => $this->session->userdata('user_id'), 'id_test' => $id_test ); $insert = $this->db->insert('students_answer', $datass); } } return true; }