I would like to make a results table on phpmyadmin with the following fields: username, score, subject and ranking. I already have a users and quiz table, users table containing the username and quiz table containing the subject. When the quiz is submitted, it displays a score by viewing the users selected answers and then checks in the quiz table if the user selected the correct answer out of a possible 4 options (fields are choice1, choice2, choice3 and answer but it is randomized when outputted). I would like it that when the user completes the quiz, it will store the result including username, subject, score and ranking (for example 10/10 = A, 5/10 = D). Please help!
Results display view:
<div id="container">
<h1>Play the Computing Quiz!</h1>
<?php $score =0; ?>
<?php $array1= array(); ?>
<?php $array2= array(); ?>
<?php $array3= array(); ?>
<?php $array4= array(); ?>
<?php $array5= array(); ?>
<?php $array6= array(); ?>
<?php $array7= array(); ?>
<?php $array8= array(); ?>
<?php foreach($checks as $checkans) { ?>
<?php array_push($array1, $checkans); } ?>
<?php foreach($results as $res) { ?>
<?php array_push($array2, $res->answer);
array_push($array3, $res->quizID);
array_push($array4, $res->question);
array_push($array5, $res->choice1);
array_push($array6, $res->choice2);
array_push($array7, $res->choice3);
array_push($array8, $res->answer);
} ?>
<?php
for ($x=0; $x <10; $x++) { ?>
<form method="post" action="<?php echo base_url();?>index.php/welcome/index">
<p><?=$array4[$x]?></p>
<?php if ($array2[$x]!=$array1[$x]) { ?>
<p><span style="background-color: #FF9C9E"><?=$array1[$x]?></span></p>
<p><span style="background-color: #ADFFB4"><?=$array2[$x]?></span></p>
<?php } else { ?>
<p><span style="background-color: #ADFFB4"><?=$array1[$x]?></span></p>
<?php $score = $score + 1; ?>
<?php } } ?>
<br><br>
<p><b>Your Score: </b></p>
<p><b><?=$score?>/10</b></p>
<input type="submit" value="Play Again!">
</form>
</div>
</body>
</html>
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Questions extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->database();
}
public function quizdisplay()
{
$this->load->model('quizmodel');
$this->data['questions'] = $this->quizmodel->getQuestions();
$this->load->view('play_quiz', $this->data);
}
public function resultdisplay()
{
$this->data['checks'] = array(
'ques1' => $this->input->post('quizid1'),
'ques2' => $this->input->post('quizid2'),
'ques3' => $this->input->post('quizid3'),
'ques4' => $this->input->post('quizid4'),
'ques5' => $this->input->post('quizid5'),
'ques6' => $this->input->post('quizid6'),
'ques7' => $this->input->post('quizid7'),
'ques8' => $this->input->post('quizid8'),
'ques9' => $this->input->post('quizid9'),
'ques10' => $this->input->post('quizid10'),
);
$this->load->model('quizmodel');
$this->data['results'] = $this->quizmodel->getQuestions();
$this->load->view('result_display', $this->data);
}
}
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class quizmodel extends CI_Model {
public function getQuestions()
{
$this->db->select("quizID, question, choice1, choice2, choice3, answer, subject");
$this->db->from("quiz");
$this->db->where("subject",'computing');
$query = $this->db->get();
return $query->result();
$num_data_returned = $query->num_rows;
if ($num_data_returned < 1) {
echo "There is no data in the database";
exit();
}
}
}