Fik lige lyst til at lave en klasse til formålet
quiz.class.php:
<?php
class Quiz
{
//QUIZ NAME
private $QuizName;
//REPLY POINTS ARE SET FROM A TO D
private $ReplyPoints = array("A","B","C","D");
//MINIMUM REPLYES
private $MinReply = 2;
private $MaxQuestions = 9;
//CONTAIN QUESTIONS
private $Questions;
//BUTTON NAME
private $ButtonName = "Name";
//BUTTON VALUE
private $ButtonValue = "Show result";
//QUESTION NUMBER (STARTS AT 1)
private $QuestionNumber = 1;
private $Error = 0;
//CONSTRUCTOR
public function __construct($QuizName = "Quiz")
{
$this->QuizName = $QuizName;
}
//SET QUESTION
public function AddQuestion($Question, $Reply, $Answer)
{
if($this->Error == 0)
{
//CHECK IF REPLY IS A ARRAY
if(is_array($Reply) && count($Reply) >= $this->MinReply && count($Reply) <= count($this->ReplyPoints))
{
//CHECK IF QUESTION IS A STRING
if(is_string($Question))
{
//CHECK IF $Answer IS IN $ReplyPoints
if(in_array($Answer, $this->ReplyPoints))
{
//CHECK MAXIMUM QUESTIONS
if($this->MaxQuestions >= $this->QuestionNumber)
{
//SET QUESTION
$this->Questions[$this->QuestionNumber]['Question'] = $Question;
//SET ANSWER
$this->Questions[$this->QuestionNumber]['Answer'] = $Answer;
for ($i = 0; $i <= count($Reply)-1; $i++) {
$this->Questions[$this->QuestionNumber]['Reply'][$this->ReplyPoints[$i]] = $Reply[$i];
}
$this->QuestionNumber++;
}else{
$this->Error(100);
}
}else{
$this->Error(101);
}
}else{
$this->Error(102);
}
}else{
$this->Error(103);
}
}
}
public function ShowQuestions()
{
//SHOW IF THE ERROR IS NOT SET
if($this->Error == 0)
{
echo "<h1>".$this->QuizName."</h1>\n";
echo "<form action='' method='POST'>\n";
echo "<ol>\n";
for ($i = 1; $i <= count($this->Questions); $i++) {
//IF NOT FIRST QUESTION
if($i != 1)
{
echo "\n<br/>";
}
echo"<li>".$this->Questions[$i][Question]."</li><br/>\n";
//RUNS THROUGH THE REPLYES
for ($x = 0; $x <= count($this->Questions[$i][Reply])-1; $x++) {
echo $this->ReplyPoints[$x].".<input type='radio' name='Question".$i."' value='".$this->ReplyPoints[$x]."'/>".$this->Questions[$i][Reply][$this->ReplyPoints[$x]]."<br/>\n";
}
}
echo "</ol>\n";
echo "<input type='submit' value='".$this->ButtonValue."' name='".$this->ButtonName."'/>\n";
echo "</form>\n";
}
}
public function ShowAnswers()
{
//IF BOTTON IS PRESSED
if(isset($_POST[$this->ButtonName]))
{
$AnswerRight = 0;
for ($x = 1; $x <= count($this->Questions); $x++) {
if($this->Questions[$x][Answer] == $_POST['Question'.$x]){
$AnswerRight++;
}
}
echo "You got ".$AnswerRight." correct answers out of ".count($this->Questions);
}
}
public function Error($x)
{
$this->Error = 1;
switch ($x) {
case 100:
$ErrorText = "There must be maximum ".$this->MaxQuestions." questions";
break;
case 101:
$ErrorText = "The answer was not allowed, or in the class array...";
break;
case 102:
$ErrorText = "The question must be of the type string...";
break;
case 103:
$ErrorText = "Reply is not of the type array, or less than ".$this->MinReply." numbers...";
break;
default:
$ErrorText = "Error found";
}
echo "<div id='Error'>".$ErrorText."</div>";
}
}
?>
quiz.php:
<?php
include("quiz.class.php");
$Quiz = new Quiz("Grønlands indbyggere");
$Quiz->AddQuestion("Hvor mange indbyggere er der i Nuuk?",array("ca.8000","ca.15000","ca.35000","ca.100000"),"A");
$Quiz->AddQuestion("Hvor mange indbyggere er der i Sisimiut?",array("ca.5600","ca.10800","ca.15000"),"A");
$Quiz->AddQuestion("Hvor mange indbyggere er der i Qaqortoq?",array("ca.2500","ca.3400","ca.6300"),"A");
$Quiz->AddQuestion("Hvor mange indbyggere er der i Grønland?",array("ca.52400","ca.57600","ca.61250"),"A");
$Quiz->ShowQuestions();
$Quiz->ShowAnswers();
?>
Hvis der er spørgsmål til overstående spørg gerne...