I will try and make this as detailed as possible because ive been marked down before. There is a select quiz file outside of the codeigniter framework, it then takes you to 1 of 5 quizzes. Below is the computing quiz (each are the same apart from the questions and answers). The files below are stored in the view folder. The Play Quiz file is used to show all of the questions and answers. It gets the questions and answers from a database called multi_login and from a table called quiz. When the user has selected the answers and clicked submit, it will run the next block of code ive put down below. It uses arrays to check whether the answer is right or wrong, if it is right then it sets the background colour to green and adds 1 to score. The total of score is then displayed at the bottom of the quiz to show the users score. I have already used the model folders file at the bottom of the page, to select the table i want "quiz". I want to be able to store the score's number after the user has completed the quiz to be stored in a table called report into a column called score. Please help, i am using phpmyadmin for my database etc. I hope this is detailed enough, if not just comment.
Play Quiz code (for explanation):
<h1>Play the Computing Quiz!</h1>
<form method="post" action="<?php echo base_url();?>index.php/Questions/resultdisplay">
<?php foreach($questions as $row) { ?>
<?php $ans_array = array($row->choice1, $row->choice2, $row->choice3, $row->answer);
shuffle($ans_array); ?>
<p><?=$row->question?></p>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[0]?>" required> <?=$ans_array[0]?><br>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[1]?>"> <?=$ans_array[1]?><br>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[2]?>"> <?=$ans_array[2]?><br>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[3]?>"> <?=$ans_array[3]?><br>
<?php } ?>
<br><br>
<input type="submit" value="Submit!">
</form>
After the submit button is clicked, it will sent the user to this page:
<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>
Model file:
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();
}
}
}