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

如何获取与变量关联的php计数器内容,php,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个问题。问题和答案在变量中定义如下:

$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)";
我想做的是向他们展示他们选择的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>";
/***问题12**//
打印“

”; 打印“$q12”; 打印“”; 如果($_POST['answer12']==$do) 打印“$do”; 其他的 打印“$do”; 如果($_POST['answer12']==$didnt){ 打印“$didnt”; $correct++; } 其他的 打印“$didnt”; 如果($_POST['answer12']==$doesnt) 打印“$doesnt”; 其他的 打印“$doesnt”; 如果($_POST['answer12']==$did) 打印“$did”; 其他的 打印“$did”; 打印“”; //***检查答案***// foreach($\以$value形式发布){ if(isset(价值)){ $done++; } } if($done!=7)//将其设置为比问题和答案的数量高1 打印“”; if($correct 0)&&($done<7))//将其设置为比问题和答案的数量高1 打印“您尚未回答所有问题。请完成测验并重新提交您的答案。

”; 如果($done==7)和($correct>4)) {//将此设置为比问题和答案的数量高1 打印“您的分数为$correct/6正确。

”;//将此设置为相同数量的问题和答案 如果($correct==0) $correct='0'; 其他的 { 打印“正确答案:

”; 打印“$a7

”; 打印“$a8

”; 打印“$a9

”; 打印“$a10

”; 打印“$a11

”; 打印“$a12

”;
正如其他人所说,您可以大大改进代码,这将是一个很好的开端

一种简单的方法是添加一个
$correctAnswers=“”;
变量,每次使用
$correct++;
添加
$correctAnswers.=“$a1

”;
等等


然后你可以用
print$correctAnswers;

$a7
$q7
显示它们。你需要了解数组。
$a[1]='answer1';$a[2]='answer2'
等。最好使用
array()
而不是使用变量,例如使用问题的
array()
,以及其他
array()
将包含每个问题的答案。
    $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);   
    $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);