mercredi 2 mai 2018

If statement not running through array of answers, rather only reading first

recently I added a print statement to an if statement declaring that if the user entered the correct answer tot the current question displayed print the user's answer if they are correct and print out what the original answer to the question is. In order to do this since I have an array like so:

$questionsAndAnwsers = array(array("question" => " What early cartoon character created by the Disney Studio was Mickey Mouse based off of ?", "answer" => "Oswald the Lucky Rabbit"),

                       array("question" => "Who invented the first TV ? Please use full name.", "answer" => "Philo Taylor Farnsworth"));

I declared the current answer to equal the current question and answer:

$currentAnswer = $questionsAndAnwsers[$currentQuestion]["answer"];

With this I finally set up the if statement to do the actual checking and this is where I know it doesn't work my setup because it no longer checks the answer correctly and as soon as the second question comes up it is wrong even with the correct answer. Here is my if statement:

if($_POST["guess"] == $currentAnswer){
     $currentQuestion++;
     $guess = $_POST['guess'];
     print ("Your answer: $guess <br>"); 
     print("The answer expected: $currentAnswer<br>");  
     print("Answer Correct<br>");
     print("Next Question Below<br>");
   } 
   else {
     $currentQuestion=0;
     $guess = $_POST['guess'];
     print ("Your answer: $guess <br>");   
     print("You have failed..<br>"); 
   }

After realizing any question after the first one fails I am thinking that my setup for the current answer is not correct and all it is calling is possibly the first answer in the set which is a huge issue as I need the answer to be changing with each new question. I thought I understood this and it would work but I am either making a simple mistake or I am misunderstanding what I need to do.

Does anyone have any advice how to achieve the current answer equaling each separate answer as the questions change.

Full code (just for reference):

<html>
<head>
<style>
#main{
  font-family:"Times New Roman", Times, serif; 
  font-size:2em;
  text-align:center;
}
</style>
</head>
<body>
<?php
$questionsAndAnwsers = array(array("question" => " What early cartoon character created by the Disney Studio was Mickey Mouse based off of ?",  "answer" => "Oswald the Lucky Rabbit"),

array("question" => "Who invented the first TV ? Please use full name.", "answer" => "Philo Taylor Farnsworth"),

array("question" => "When was Warner Brothers founded ? Format: Month Day, Year", "answer" => "April 4, 1923"), 

array("question" => "When was Superman's first appearance date ? Format: Month Year", "answer" => "June 1938"),

array("question" => "What does the acronym OWN, for the cable television channel, stand for ?", "answer" => "Oprah Winfrey Network"),

array("question" => "What type of dog is Scooby Doo from the cartoon series, Scooby Doo ?", "answer" => "Great Dane"),

array("question" => "What type of food does Garfield the cat love ?", "answer" => "Lasagna"), 

array("question" => "How many Pokemon were in Generation I ?", "answer" => "151"), 

array("question" => "What popular cartoon/show is Woodstock from ?", "answer" => "Peanuts"), 

array("question" => "What was Jim Henson's first puppet series back in 1955 ?", "answer" => "Sam and Friends"), 

array("question" => "Who created the Mighty Morphin Power Rangers ?", "answer" => "Haim Saban"), 

array("question" => "How many Back to the Future movies were there ?", "answer" => "3"), 

array("question" => "What football quarterback had a short-lived television show that premiered in 1969 ?", "answer" => "Joe Namath"),

array("question" => "Where there any R rated movies in 1961 ?", "answer" => "No"), 

array("question" => "In what year did the animal-documentary series Wild Kingdom premiere ?", "answer" => "1963"), 

array("question" => "What sit-com was about two bumbling police men and their squad car ?", "answer" => "Car 54, Where Are You")); 


// current question
$currentQuestion = 0;
$currentAnswer = $questionsAndAnwsers[$currentQuestion]["answer"];

if(isset($_POST["currentQuestion"])){
     $currentQuestion = $_POST["currentQuestion"];

     if($currentQuestion==15){
        header("Location:  http://students.purchase.edu/martin.mcnicholas/scriptingfortheweb/index2.html"); /* Redirect browser */
        exit();
     }else if($_POST["guess"] == $currentAnswer){
         $currentQuestion++;
         $guess = $_POST['guess'];
         print ("Your answer: $guess <br>"); 
         print("The answer expected: $currentAnswer<br>");  
         print("Answer Correct<br>");
         print("Next Question Below<br>");
       } 
       else {
         $currentQuestion=0;
         $guess = $_POST['guess'];
         print ("Your answer: $guess <br>");   
         print("You have failed..<br>"); 
       }
    }
?>


<div id=main>
<br><br><br><br>
<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="" placeholder="Answer...">
    <input type="submit" value="Next Question">
</form>
</div>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire