如果声明没有通过答案数组运行,而只是首先阅读

时间:2018-05-02 21:39:30

标签: php arrays if-statement

最近我在if语句中添加了一个print语句,声明如果用户输入正确答案,则显示当前问题,如果用户正确则打印用户的答案并打印出问题的原始答案是什么。为了做到这一点,因为我有一个像这样的数组:

$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"));

我宣布当前的答案与当前的问题和答案相同:

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

有了这个,我终于设置了if语句进行实际检查,这就是我知道它不能正常工作的地方,因为它不再正确检查答案,一旦第二个问题出现即使有正确的答案也是错的。这是我的if语句:

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>"); 
   }

在第一个问题失败后意识到任何问题之后我认为我对当前答案的设置不正确而且所有调用可能是该集合中的第一个答案,这是一个很大的问题,因为我需要答案是随着每个新问题的变化。我以为我理解这一点并且它会起作用,但我要么犯了一个简单的错误,要么我误解了我需要做的事情。

当问题发生变化时,有没有人有任何建议如何达到当前答案等于每个单独的答案。

完整代码(仅供参考):

<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>

1 个答案:

答案 0 :(得分:0)

如果已提交$currentAnswer,您需要重新定义$_POST["currentQuestion"]变量。否则$currentAnswer将始终设置为0(这就是为什么只有第一个答案有效)。

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

     //Add this line
     $currentAnswer = $questionsAndAnwsers[$currentQuestion]["answer"]
     //Continue your code here

     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>"); 
       }
    }

同样,检查$questionsAndAnwsers的数组索引和答案是否确实存在是一个好习惯:

if(isset($_POST["currentQuestion"])){
     $currentQuestion = $_POST["currentQuestion"];
     if(isset($questionsAndAnwsers[$currentQuestion])){
       $currentAnswer = $questionsAndAnwsers[$currentQuestion]["answer"]
       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>"); 
         }       
     }else{
       //Do some error handling here 
       exit("Question not found!");
     }

    }