如何比较输入与数组php

时间:2018-09-09 22:29:02

标签: php

我正在做一个相反的测验。提示时会提示一个字。您必须反面回答。我用单词列表制作了两个数组,并提出了一组预定义的示例问题。我的想法是,我将把这个词与一个相反的词进行比较,并且会一直继续下去,直到找到正确的输入词为止。

由于某种原因,它不起作用。我对PHP还是很陌生,我敢肯定有一种更简单的方法可以做到这一点。目前,这是我必须使用的。

我也想使用数组而不是MySQL

    <?php
    $wl1 = array('Hot', 'Summer', 'Hard', 'Dry', 'Simple', 'Light', 'Weak', 'Male', 'Sad', 'Win', 'Small', 'Ignore', 'Buy', 'Succeed', 'Reject', 'Prevent',
    'Exclude');
    $wl2 = array('Cold', 'Winter', 'Soft', 'Wet', 'Complex', 'Darkness', 'Strong', 'Female', 'Happy', 'Lose', 'Big', 'Pay Attention', 'Sell', 'Fail', 'Accept',
    'Allow', 'Include');

    $compl = array("Hot is to cold",
                   "Summer is to winter",
                   "Hard is to soft",
                   "Dry is to wet",
                   "Simple is to complex",
                   "Light is to darkness",
                   "Weak is to strong",
                   "Male is to female",
                   "Sad is to happy",
                   "Win is to lose",
                   "Small is to big",
                   "Ignore is to pay attention",
                   "Buy is to sell",
                   "Succeed is to fail",
                   "Reject is to accept",
                   "Prevent is to allow",
                   "Exclude is to include");

    $complr = $compl[array_rand($compl)];
    $wl2r = $wl2[array_rand($wl1)];
    $q = $complr . " as ".$wl2r." is to "."<br>";
    echo $q;

    if(isset($_POST['submit'])){
        $score = 0;
        $answer = $_POST['answer'];
            if($wl2r == "Cold" && $answer == "Hot"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Winter" && $answer == "Summer"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Soft" && $answer == "Hard"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Wet" && $answer == "Dry"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Complex" && $answer == "Simple"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Darkness" && $answer == "Light"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Strong" && $answer == "Weak"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Female" && $answer == "Male"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Happy" && $answer == "Sad"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Lose" && $answer == "Win"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Big" && $answer == "Small"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Pay Attention" && $answer == "Ignore"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Sell" && $answer == "Buy"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Fail" && $answer == "Succeed"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Accept" && $answer == "Reject"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Allow" && $answer == "Prevent"){
                echo "Correct";
                $score++;
            }
            if($wl2r == "Include" && $answer == "Exclude"){
                echo "Correct";
                $score++;
            }
        echo $score;


    }







?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Opposites</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
    <div class="form-group">
        <form action="task3.php" method="post">
            Enter your Answer <input type="text" name="answer">
            <input type="submit" name='submit'>
        </form>
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

一个问题是,如果没有数据库,就无法在页面加载之间持久保存程序所需的状态数据。例如,提交之后,就无法知道在前一页页面加载中进行了哪个随机选择或得分是多少。最好的方法是使用数据库或Ajax,但除此之外,可以(但不是很优雅)使用隐藏的输入字段轻松传递数据。您还可以探索本地存储和JavaScript,这些都不会太简单。

关于您的游戏逻辑,请考虑使用关联数组。代替数组键是数字序列0、1、2 ...,而是使用第一对的字符串作为检索其伙伴的键。这消除了巨大的条件块,大量的键入和可能的错误:

library(tidyverse)

df <- read_csv("~/Desktop/dummy_data.csv") %>% 
  mutate(id = 1:n()) %>% 
  spread(employee, task) %>% 
  select(-id)

# A tibble: 40 x 6
   e1    e2    e3    e4    e5    e6   
   <chr> <chr> <chr> <chr> <chr> <chr>
 1 NA    NA    NA    t1    NA    NA   
 2 NA    NA    t2    NA    NA    NA   
 3 NA    NA    NA    NA    t3    NA   
 4 NA    NA    NA    NA    NA    t4   
 5 NA    NA    NA    NA    t5    NA   
 6 NA    NA    t6    NA    NA    NA   
 7 NA    NA    NA    NA    NA    t7   
 8 NA    NA    t8    NA    NA    NA   
 9 t9    NA    NA    NA    NA    NA   
10 t10   NA    NA    NA    NA    NA   
# ... with 30 more rows

这是要测试的相关部分的命令行repl

此外,如果您想访问反向数组,例如查找<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Opposites</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> </head> <body> <?php $compliments = [ 'Hot' => 'Cold', 'Summer' => 'Winter', 'Hard' => 'Soft', 'Dry' => 'Wet', 'Simple' => 'Complex', 'Light' => 'Dark', 'Weak' => 'Strong', 'Male' => 'Female', 'Sad' => 'Happy', 'Win' => 'Lose', 'Small' => 'Big', 'Ignore' => 'Pay Attention', 'Buy' => 'Sell', 'Succeed' => 'Fail', 'Reject' => 'Accept', 'Prevent' => 'Allow', 'Exclude' => 'Include' ]; $score = isset($_POST['score']) ? (int)$_POST['score'] : 0; if (isset($_POST['answer']) && isset($_POST['question'])) { if ($_POST['answer'] === $compliments[$_POST['question']]) { echo "<div>Correct, " . $_POST['question'] . " is to " . $_POST['answer'] . ".</div>"; $score++; } else { echo "<div>Incorrect. " . $_POST['question'] . " is to " . $compliments[$_POST['question']] . ", not to " . $_POST['answer'] . ".</div>"; } } $sample = array_rand($compliments); $test = array_rand($compliments); echo "<div>Score: $score</div>"; echo "<div>$compliments[$sample] is to $sample as $test is to </div>"; ?> <div class="form-group"> <form action="task3.php" method="post"> Enter your Answer <input type="text" name="answer"> <input type="hidden" name="question" value="<?= isset($test) ? $test : "" ?>"></input> <input type="hidden" name="score" value="<?= isset($score) ? $score: "" ?>"></input> <input type="submit" name='submit'> </form> </div> </body> </html> 以获得'Cold',使用array_flip