如何获取与变量相关的php计数器内容

时间:2014-04-24 16:30:02

标签: php

我写了一个测验脚本,一切正常。我现在要做的是进行一些微调。

有6个问题。问题和答案在这样的变量中定义:

$q7="(7) __________ you like the red one or the blue one? (Positive, simple present)";
$a7="(7) <strong>Do</strong> you like the red one or the blue one? (Positive, simple present)";

当这个人回答每个问题时,有一个计数器可以递增每个正确的答案。

我可以查看计数器并看到它包含正确数量的响应,但它只是一个数字。

如果这个人得到6分中的3分,我会向他们展示这样的分数 -

You got 3/6 correct.

我想要做的是向他们展示他们选择的3个正确答案,而不是错误答案。

这可能吗?如果计数器知道有3个正确答案,我该如何获得该信息?那些特定的变量?

我如何得到这个 - 如果$ counter包含4个或更少的正确答案,则显示这些答案。

// *** question 12 *** //
print "<p>&nbsp;</p><table id='english_nb'><tr>";
print "<th>$q12</th></tr></table>";
print "<table id='english_nb'><tr>";
if ($_POST['answer12']==$do)
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$do' />$do</td>";
else
print "<td width='25%'><input type='radio' name='answer12' value='$do' />$do</td>";
if ($_POST['answer12']==$didnt){
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$didnt' />$didnt</td>";
$correct++;
}    
else
print "<td width='25%'><input type='radio' name='answer12' value='$didnt' />$didnt</td>";
if ($_POST['answer12']==$doesnt)
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$doesnt' />$doesnt</td>";
else
print "<td width='25%'><input type='radio' name='answer12' value='$doesnt' />$doesnt</td>";
if ($_POST['answer12']==$did)
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$did' />$did</td>";
else
print "<td width='25%'><input type='radio' name='answer12' value='$did' />$did</td>";
print "</tr></table>";
// *** check the answers *** //   
foreach ($_POST as $value){
if (isset ($value)){
$done++;
}
}
if ($done !=7) //set this to 1 higher than the number of questions and answers
print "<input type='submit' name='submit' value='Check answers' />";
if (($correct<5)&&($done==7)) //set this to the number of minimum correct answers
print "<p>You should review the information and try the quiz again.</p>";
    print "<p>Your score is $correct/6 correct.</p>"; //set this to the same number of questions and answers

    if (($done > 0)&&($done < 7)) //set this to 1 higher than the number of questions and answers
print "<p>You haven't answered all the questions. Please finish the quiz and re-submit your answers.</p>";
if(($done==7)&&($correct>4))
    { //set this to 1 higher than the number of questions and answers
print "<p>Your score is $correct/6 correct.</p>"; //set this to the same number of questions and answers
if ($correct==0)
$correct='0';
else 
    {
print "<p>The correct answers:</p>";
print "<p>$a7</p>"; 
print "<p>$a8</p>";
print "<p>$a9</p>";   
print "<p>$a10</p>";
print "<p>$a11</p>";
print "<p>$a12</p>";

2 个答案:

答案 0 :(得分:0)

你可以大量改进你的代码,arrays会像其他人所说的那样开始。

执行所需操作的简单方法是添加$correctAnswers = "";变量,每次使用$correct++;添加$correctAnswers .= "<p>$a1</p>";等等。

然后您可以使用print $correctAnswers;

显示它们

答案 1 :(得分:0)

    $q=array($q1,$q2,....);    //questions array
    $a=array($a1,$a2,....);    //answers array
    correctAnswer=array();     //array of correct answers. empty at start

    if (question=$q[0] && answer=$a[0]){  //if question and answer match 
      $correctAnswer[]=$a[0];   //adds the correct answer to the array
    }
    else{
       // do something if the q &a doesnt match
    }

    $correct_answer_count = count($correctAnswer);