<?PHP
// Total Questions:
// HELP: Set this to number of questions you have.
$number_of_questions = 3;
// The Questions:
$quiz = array( '1' => array ( 'question' => "What's brown & sounds like a bell",
'choices' => array( 'a' => 'Dung', 'b' => 'Doo', 'c' => 'Ding', 'd' => 'Dong' ),
'answer' => "a" ),
'2' => array ( 'question' => "What's brown & sticky",
'choices' => array( 'a' => 'Bogroll', 'b' => 'Doodah', 'c' => 'Stick', 'd' => 'Darkness' ),
'answer' => "c" ),
'3' => array ( 'question' => "What black & white & red all over",
'choices' => array( 'a' => 'Yer Maw', 'b' => 'Yer Paw', 'c' => 'Yer Face', 'd' => 'A Nun falling down the stairs.' ),
'answer' => "d" ),
/* '3' => array ( 'question' => "<%YOUR_QUESTION_HERE%>",
'choices' => array( 'a' => '<%CHOICE_1%>', 'b' => '<%CHOICE_2%>', 'c' => '<%CHOICE_3%>', 'd' => '<%CHOICE_4%>' ),
'answer' => "<%CORRECT_ANSWER_LETTER_HERE%>" ),*/
);
// +-----
// LOGIC
// +-----
if ( ( isset($_POST['action'] ) ) && ( $_POST['action'] == "go" ) ) {
$current_question = $_POST['current_question'];
$next_question = $current_question + 1;
$score = $_POST['score'];
// GET answer, and mark if right.
$answer = $quiz[ $current_question ]['answer'];
if ( $answer == $_POST["q" .$current_question ] ) {
$score = $_POST['score'] + 1;
}
// Increment ready for NEXT question:
$current_question = $next_question;
$next_question = $current_question + 1;
if( $current_question > $number_of_questions ) {
exit("You scored $score out of $number_of_questions. <a href='?'>Restart Quiz</a>");
}
}
else {
$current_question = "1";
$next_question = $current_question + 1;
$score = "0";
}
// +-------------
// THE HTML CODE
// +-------------
echo "<form method='POST'>\n
<input type='hidden' name='action' value='go' />
<input type='hidden' name='current_question' value='$current_question' />
<input type='hidden' name='next_question' value='$next_question' />
<input type='hidden' name='score' value='$score' />\n";
// OUTPUT Question...
echo "{$quiz[ $current_question ]['question']}?<br />\n";
// OUTPUT Choices...
foreach( $quiz[ $current_question]['choices'] as $k => $v ) {
echo "<input type='radio' name='q$current_question' value='$k'> $k: $v<br />\n";
}
echo "<p><input type='submit' value='Next Question'></p>\n";
exit();
?>
-----------------------------------
It keeps messing up my page, there must be a error in it.
Can someone please paste the full code here, fixed up?
Thanks
// Total Questions:
// HELP: Set this to number of questions you have.
$number_of_questions = 3;
// The Questions:
$quiz = array( '1' => array ( 'question' => "What's brown & sounds like a bell",
'choices' => array( 'a' => 'Dung', 'b' => 'Doo', 'c' => 'Ding', 'd' => 'Dong' ),
'answer' => "a" ),
'2' => array ( 'question' => "What's brown & sticky",
'choices' => array( 'a' => 'Bogroll', 'b' => 'Doodah', 'c' => 'Stick', 'd' => 'Darkness' ),
'answer' => "c" ),
'3' => array ( 'question' => "What black & white & red all over",
'choices' => array( 'a' => 'Yer Maw', 'b' => 'Yer Paw', 'c' => 'Yer Face', 'd' => 'A Nun falling down the stairs.' ),
'answer' => "d" ),
/* '3' => array ( 'question' => "<%YOUR_QUESTION_HERE%>",
'choices' => array( 'a' => '<%CHOICE_1%>', 'b' => '<%CHOICE_2%>', 'c' => '<%CHOICE_3%>', 'd' => '<%CHOICE_4%>' ),
'answer' => "<%CORRECT_ANSWER_LETTER_HERE%>" ),*/
);
// +-----
// LOGIC
// +-----
if ( ( isset($_POST['action'] ) ) && ( $_POST['action'] == "go" ) ) {
$current_question = $_POST['current_question'];
$next_question = $current_question + 1;
$score = $_POST['score'];
// GET answer, and mark if right.
$answer = $quiz[ $current_question ]['answer'];
if ( $answer == $_POST["q" .$current_question ] ) {
$score = $_POST['score'] + 1;
}
// Increment ready for NEXT question:
$current_question = $next_question;
$next_question = $current_question + 1;
if( $current_question > $number_of_questions ) {
exit("You scored $score out of $number_of_questions. <a href='?'>Restart Quiz</a>");
}
}
else {
$current_question = "1";
$next_question = $current_question + 1;
$score = "0";
}
// +-------------
// THE HTML CODE
// +-------------
echo "<form method='POST'>\n
<input type='hidden' name='action' value='go' />
<input type='hidden' name='current_question' value='$current_question' />
<input type='hidden' name='next_question' value='$next_question' />
<input type='hidden' name='score' value='$score' />\n";
// OUTPUT Question...
echo "{$quiz[ $current_question ]['question']}?<br />\n";
// OUTPUT Choices...
foreach( $quiz[ $current_question]['choices'] as $k => $v ) {
echo "<input type='radio' name='q$current_question' value='$k'> $k: $v<br />\n";
}
echo "<p><input type='submit' value='Next Question'></p>\n";
exit();
?>
-----------------------------------
It keeps messing up my page, there must be a error in it.
Can someone please paste the full code here, fixed up?
Thanks