Good day, I'm trying to build an online quiz for my website. i want to do it oop style but i'm having problems in certain areas but i'll need help.
this is the class code
class Quiz{
// method to get the questions and answers
public function getQuestion(){
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="nigeriag_mytest"; // Database name
$tbl_name="question_list"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB");
$data = mysql_query("SELECT * FROM $tbl_name WHERE 1 ORDER BY RAND() LIMIT 1;") or die(mysql_error());
while ($info=mysql_fetch_array($data)) {
echo "<form name = 'form1 method = 'post' action=''>";
echo "<label>".$info['question']."</label>";
echo "<br />";
echo "<input type= 'Radio' name= 'A'>".$info['opt1'];
echo "<br />";
echo "<input type= 'Radio' name= 'A'>".$info['opt2'];
echo "<br />";
echo "<input type= 'Radio' name= 'A'>".$info['opt3'];
echo "<br />";
echo "<input type= 'Radio' name= 'A'>".$info['opt4'];
echo "<br />";
echo "<input type ='submit' name= 'submit1' value ='Next'>";
//echo $info['answer'];
echo "</form>";
$answer = $info['answer'];
}
}
i call this class code in an page say 'x'..
My problem is calculating the score of who ever takes the quiz. Theoretically i believe i should compare the selected value with the correct answer when the form is posted and increment a $score variable by 1 but i dont know where this comparison should take place(class file or page x)
page x below
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
include('quizClass.php');
$x = new Quiz;
$x->getQuestion();
?>
</body>
</html>
Hope my question makes sense to someone.