我可以使用这个表格进行测验:
<form action="" method="post">
<p><strong>HOOFDSTAD VAN PERU?</strong><br>
<select name="v1">
<option value="0">Kies een antwoord</option>
<option value="1">SANTIAGO</option>
<option value="2">PERU-STAD</option>
<option value="3">MANAGUA</option>
<option value="4">LIMA</option>
</select>
<hr>
<p><strong>Hoofdplaats van Provincie Luxemburg?</strong><br>
<select name="v2">
<option value="0">Kies een antwoord</option>
<option value="1">EUPEN</option>
<option value="2">ARLON</option>
<option value="3">GIVET</option>
<option value="4">MARCINELLE</option>
</select>
<hr>
and so on...
我将此代码用于正确的值
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['v1'] == '4') {
$v1 = 1;
}
else {
$v1 = 0;
}
if($_POST['v2'] == '2') {
$v2 = 1;
}
else {
$v2 = 0;
}
// and so on...
// count the number of times that $v1 till $v5 == 1 and echo the number
}
问题1:如何计算值等于1的次数? 因为输出应该是这样的:你回答的问题中有7个是正确的
问题2:让我说测验中有100个问题;它可以比我现在这样做更有效吗?使用if / else?
答案 0 :(得分:1)
$ar = (...)//array of correct answers
$counter = 0;
for($i=0; $i<$noLines; $i++){
if($_POST['v'.$i] == $ar[$i])
$counter += 1;
}
echo $counter." of your answered questions are correct";