0

I am calling some variables from MySQL database and comparing them with some variables which are being POSTED via a HTML form from a previous page. They are actually correct answers and given answers.

My current structure looks like this:

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

The structure was not really hectic until there were only 3 questions. But now the questions are increased to 100. I want to know how to do something like this:

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;
1
  • why not do a foreach() loop through the post array Commented Jul 11, 2013 at 0:20

2 Answers 2

3

First off, consider using arrays instead; they're meant for this kind of repetitive stuff:

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

If that's really not possible, you could use variable variables:

if (${"ans$i"} == ${"que$i"}) {
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could also use more complex arrays to both set up the question form and to then check for the answers. Consider if you had this array:

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

In the page where the questions are asked, you could create the form by going through the array and printing each question with it's answers. Somewhat like:

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

The data could then be checked in the check_answers.php file by comparing the "correct_answers" element in each question with the value provided by the form.

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

You could even extend that to show precicely which questions the user got wrong and which right, as the indexes for both are stored in the arrays.

The $questions array, although being static in the above example, could also easily be generated from a MySQL schema.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.