This code is giving med problem. It dos not stop when the last question has been answered. It just give me this error:
Warning: Undefined array key 2 in /customers/5/8/b/kbekker.dk/httpd.www/Fag/Dansk/Opgaver/Ordklasser/find_navneord_001.php on line 71 Warning: Trying to access array offset on value of type null in /customers/5/8/b/kbekker.dk/httpd.www/Fag/Dansk/Opgaver/Ordklasser/find_navneord_001.php on line 71 3
which is refering to line:
<label for="question"><?php echo ($currentQuestion+1).". ". $questionsAndAnwsers[$currentQuestion]["question"];?></label>
I am a noob at php.
I have try to put a loop outside of it, but that dos not help either.
<?php
// Declare all your questions and answers as multi dimensional array
$questionsAndAnwsers = array(array("question" => "1. How many sides does a triangle have?", "answer" => "three"),
array("question" => "2. How many sides does a square have?", "answer" => "four"));
// current question
$currentQuestion = 0;
if(isset($_POST["currentQuestion"])){
$currentQuestion = $_POST["currentQuestion"];
if($_POST["guess"] == $questionsAndAnwsers[$currentQuestion]["answer"]){
// answer was correct, increment your question-counter for the next question
$currentQuestion++;
// print success message
echo "Det var rigtigt";
} else {
// question was wrong, counter stays as it is, so same question will be printed
// print failure message
echo "prøv igen";
}
}
?>
<form method="POST" action="">
<label for="question"><?php echo ($currentQuestion+1).". ". $questionsAndAnwsers[$currentQuestion]["question"];?></label>
<input type="hidden" name="currentQuestion" value="<?php echo $currentQuestion;?>">
<input type="text" name="guess" value="">
<input type="submit">
</form>
1