0

我正在从 MySQL 数据库中调用一些变量,并将它们与一些变量进行比较,这些变量是POSTED通过上一页的 HTML 表单。它们实际上是正确的答案和给出的答案。

我当前的结构如下所示:

if($ans1==$que1){
echo"TRUE";
}
if($ans2==$que2){
echo"TRUE";
}
if($ans3==$que3){
echo"TRUE";
}
//AND SO ON...

直到只有 3 个问题,结构才真正忙碌。但现在问题增加到 100。我想知道如何做这样的事情:

for(i=1; i=100; i++){
if($ans.$i==$que.$i){
echo"TRUE";
$total_correct_ans=$total_correct_ans+1;
}
}
echo"Total correct answers are ". $total_correct_ans;
4

2 回答 2

3

首先,考虑改用数组;它们适用于这种重复的东西:

// $answers = [1, 3, 2];
// $questions = [1, 2, 3];
for ($i = 0; $i < count($answers); ++$i) {
    if ($answers[$i] == $questions[$i]) { 
        echo "TRUE" 
    };
}

如果这真的不可能,您可以使用变量 variables

if (${"ans$i"} == ${"que$i"}) {
}
于 2013-07-11T00:21:29.680 回答
0

您还可以使用更复杂的数组来设置问题表单,然后检查答案。考虑一下你是否有这个数组:

$questions = array(
    array(
        "question" => "What color is the sky",
        "answers" => array(
            "red",
            "green",
            "blue",
            "penquine"
        ),
        "correct_answer" => 2
    ),
    array(
        "question" => "What is the bestest langugage in the world!",
        "answers" => array(
            "C#",
            "English",
            "Brainfuck",
            "PHP",
        ),
        "correct_answer" => 3
    ),
);

在提出问题的页面中,您可以通过遍历数组并打印每个问题及其答案来创建表单。有点像:

echo "<form action='check_answers.php' method='post'>\n";
foreach ($questions as $qi => $q) {
    echo "<h1>{$q["question"]}</h1>\n";
    foreach ($q["answers"] as $ai => $answer) {
        echo "<label><input type='radio' name='answers[$qi]' value='$ai'> $answer</label>\n";
    }
}
echo "<input type='submit'></form>";

然后可以check_answers.php通过将每个问题中的“correct_answers”元素与表单提供的值进行比较,在文件中检查数据。

if (isset($_POST["answers"]) && is_array($_POST["answers"])) {
    $correctAnswers = array();
    $incorrectAnswers = array();

    foreach ($_POST["answers"] as $question => $answer) {
        if ($questions[$question]["correct_answer"] == $answer) {
            $correctAnswers[] = $questions;
        }
        else {
            $incorrectAnswers[] = $question;
        }
    }

    echo "You had " . count($correctAnswers) . " correct answers.<br>\n";
    echo "You had " . count($incorrectAnswers) . " incorrect answers.<br>\n";
}

您甚至可以扩展它以准确显示用户错误的问题和正确的问题,因为两者的索引都存储在数组中。

$questions数组虽然在上面的示例中是静态的,但也可以很容易地从 MySQL 模式生成。

function generateQuestions(\PDO $pdo) {
    $questions = array();

    $sql = "SELECT id, question FROM questions";
    $result = $pdo->query($sql);
    foreach ($result as $row) {
        $question = array(
            "question" => $row["question"],
            "answers" => array(),
            "correct_answer" => 0
        );

        $answerSql = "SELECT id, answer, isCorrect FROM answers
                    WHERE question_id = {$row["id"]}";
        $answerResult = $pdo->query($answerSql);
        foreach ($answerResult as $answer) {
            $answers[$answer["id"]] = $answer["answer"];
            if ($anser["isCorrect"]) {
                $question["correct_answer"] = $answer["id"];
            }
        }

        $questions[$row["id"]] = $question;
    }

    return $question;
}
于 2013-07-11T00:42:10.953 回答