在PHP中附加带变量的字符串并循环

时间:2013-07-11 00:17:05

标签: php html

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

我目前的结构如下:

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;

2 个答案:

答案 0 :(得分:3)

首先,考虑使用数组;他们意味着这种重复的东西:

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

如果真的不可能,您可以使用variable variables

if (${"ans$i"} == ${"que$i"}) {
}

答案 1 :(得分: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>";

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

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