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.

Recommended Answers

All 7 Replies

It is a bit difficult to give you an exact answer but I think your Quiz class should have all the attributes and methods to handle the quiz, such as a method to display a question (which you already have), an attribute (an array) with correct answers for all questions, a method to check the correctness of each answer, a method to display result for each question, a method to calculate overall score, a method to store data in database (whichever data you wish to store) etc. The getQuestion() method could be split into two: one to connet to the database and the other to select and display questions to achieve esier maintainability.

First, your class is mixing to many responsibilities.
You should be passing an already established database into your quiz class if it is needed.
Second, Your class should not be responsible for rendering the form as well, this is another responsibility that is mixed.
Third, you could probably benefit from a more robust class design.

Think of the design like this:

  • A "quiz" is a composite of "questions" that are rendered into a singular form.
  • The quiz should be able to add and remove questions as well as do it in a batch fashion.
    • Questions may need to be of different types, therefore rendered individually. (Multiple Choice, Select One, Input a Value)
    • Think about a Question abstract or interface that all question types need to extend, they all have common functionality and/or methods but also specific details that need to be represented.
  • When rendering a quiz, the ultimate goal would be to have a template for each question type, and a template for a quiz as a whole which would render the question templates into a form template and create the output.

It sounds like a lot more work, but the end result with be a quiz structure where you will be able to swap pieces and parts with little to no hassle, and also add or replace the questions and templates without touching other portions of your code.

commented: nice one - I learned a lot from that. :) +14

thanks broj1 and mschroeder
mschroeder i know the class is doing too much. I just wanted to get a working model first. This is my first attempt at OO Php.

azdonald, and for me OOP is my new affinity.
I don't know for what reason you active db connection here:

public function getQuestion(){
 $host="localhost";  // Host name
 $username="root";   // Mysql username 
 $password="";   // Mysql password

Define the variables first:

<?php

class xxxx {

protected $host;
protected $database;
protected $password;
protected $username;
protected $_connect;

public function __construct($host,$username,$other vars){
$this->host = $host; // and the same way for other vars
$this->connect();
}

public function connect(){
$this->_connect = mysql_connect($this->host,...)
$this->sdb();
}
public function sdb(){
mysql_select_db($this->database);
}

}

http://www.php.net/manual/en/language.oop5.php
Read this link, that will help you to understand the oop.

thanks gorleone

First, your class is mixing to many responsibilities.
You should be passing an already established database into your quiz class if it is needed.
Second, Your class should not be responsible for rendering the form as well, this is another responsibility that is mixed.
Third, you could probably benefit from a more robust class design.

Think of the design like this:

  • A "quiz" is a composite of "questions" that are rendered into a singular form.
  • The quiz should be able to add and remove questions as well as do it in a batch fashion.
    • Questions may need to be of different types, therefore rendered individually. (Multiple Choice, Select One, Input a Value)
    • Think about a Question abstract or interface that all question types need to extend, they all have common functionality and/or methods but also specific details that need to be represented.
  • When rendering a quiz, the ultimate goal would be to have a template for each question type, and a template for a quiz as a whole which would render the question templates into a form template and create the output.

It sounds like a lot more work, but the end result with be a quiz structure where you will be able to swap pieces and parts with little to no hassle, and also add or replace the questions and templates without touching other portions of your code.

you might want to look at one of those PHP framework!

you might want to look at one of those PHP framework!

Maybe you should look at some of the cutting edge php frameworks like symfony2, Lithium or how Zend Framework 2 is developing, you will find very little if any mixing of responsibilities.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.