簡體   English   中英

如何將輸入與數組 php 進行比較

[英]How to compare input to array 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>

一個問題是,如果沒有數據庫,就不可能在頁面加載之間保留程序所需的狀態數據。 例如,在提交之后,無法知道在上一次加載頁面時進行了哪個隨機選擇或得分是多少。 執行此操作的最佳方法是使用數據庫或 Ajax,但除此之外,還可以(但不是很優雅)使用隱藏的輸入字段在不費力的情況下傳遞數據。 您還可以探索本地存儲和 JavaScript,它們不會那么笨拙。

至於您的游戲邏輯,請考慮使用關聯數組。 數組鍵不是數字序列 0、1、2...,而是使用第一對的字符串作為檢索其伙伴的鍵。 這消除了巨大的條件塊、大量輸入和可能的錯誤:

<!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'以獲取'Hot' ,請使用array_flip

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM