Witam,
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
<h3>Test:
<?php echo $test_name->name_test ?> Przedmiot:
<?php echo $subject_name->name_subject ?> <br>
Pozostały czas Y <br>
Ilość pytań:
<?php echo $max->amount ?> </h3>
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<?php echo form_open
('students_site/begin_test/'.$test_name->id_test); ?>
<?php $i=1; ?>
<?php foreach($questions_test as $question_test) : ?>
<?php echo $i ?>.
<?php echo $question_test->question ?><br>
<?php
$data['results'] = array( 'name' => 'answer'.$question_test->id_test_item,
'id' => $question_test->id_test_item,
'value' => $question_test->answerA,
'checked' => false,
'style' => 'margin:10px',
); ?>
<?php echo form_radio
( $data['results']); ?> <?php echo $question_test->answerA ?><br>
<?php
$data['results'] = array( 'name' => 'answer'.$question_test->id_test_item,
'id' => $question_test->id_test_item,
'value' => $question_test->answerB,
'checked' => false,
'style' => 'margin:10px',
); ?>
<?php echo form_radio
( $data['results']); ?> <?php echo $question_test->answerB ?><br>
<?php
$data['results'] = array( 'name' => 'answer'.$question_test->id_test_item,
'id' => $question_test->id_test_item,
'value' => $question_test->answerC,
'checked' => false,
'style' => 'margin:10px',
); ?>
<?php echo form_radio
( $data['results']); ?> <?php echo $question_test->answerC ?><br>
<?php
$data['results'] = array( 'name' => 'answer'.$question_test->id_test_item,
'id' => $question_test->id_test_item,
'value' => $question_test->answerD,
'checked' => false,
'style' => 'margin:10px',
); ?>
<?php echo form_radio
( $data['results']); ?> <?php echo $question_test->answerD ?><br><br>
<?php $i=$i+1; ?>
<?php endforeach; ?>
<br><br>
<?php
$data['results'] = array("value" => "Wyślij odpowiedzi", "name" => "submit",
"class" => "btn btn-primary"); ?>
<p>
<?php echo form_submit
($data['results']); ?> </p>
<?php echo form_hidden
('username', 'johndoe'); ?> <?php echo form_close
(); ?>
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;
}