这是我用来评估数据库驱动的测验答案的脚本:
if (isset($_POST)):
$totalCorrect = 0;
$answers = array(1 => 'A', 2 => 'Jupiter', 3 => 'C', 4 => 'D', 5 => 'A', 6 => 'C', 7 => 'C', 8 => 'C', 9 => 'B', 10 => array('A','B','C'));
foreach ($answers as $num => $answer):
$question = 'q'.$num;
if(is_array($answer) && isset($_POST[$question])){
$ans_cnt = count($answer);
$ans_value = (1 / $ans_cnt);
$post_cnt = count($_POST[$question]);
//find matches between answer array and post array
$matches = array_intersect($answer,$_POST[$question]);
$good_answers = count($matches);
//Get bad answer count, which be be subtracted from overall score
$bad_answers = 0;
foreach($_POST[$question] as $post_answer):
if(!in_array($post_answer,$answer)):
$bad_answers++;
endif;
endforeach;
if($good_answers ==3 && $bad_answers==0){
$result = 1;
}else{
$result = 0;
}
if(($post_cnt != $ans_cnt) || ($post_cnt == $ans_cnt && $ans_cnt != count($matches))){
$result = $result * $ans_value;
$totalCorrect = $totalCorrect + $result;
}else{
$totalCorrect++;
}
}elseif(isset($_POST[$question]) && strtolower($_POST[$question]) === strtolower($answer)){
$totalCorrect++;
}
endforeach;
$pct = round( (($totalCorrect/count($answers)) * 100), 0);
echo $totalCorrect.' correct ('.$pct.'%)';
endif;
它工作正常,但它有一些局限性。例如,如果测验有多个问题需要用户选择多个答案(复选框),则无效。所以我想用这个脚本替换它:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$qa = 'q'.$num;
$correct = 0;
$answers = array(1 => array('A'),
2 => array('Jupiter'),
3 => array('C'),
4 => array('D'),
5 => array('A'),
6 => array('C'),
7 => 'C',
8 => 'C',
9 => 'B',
10 => array('A','B','C'));
$total = count($answers);
foreach($answers as $k => $v){
if(is_array($v)){
if(array_diff($qa[$k], $v) == array()){
$correct++;
}
} else if($qa[$k] === $v){
$correct++;
}
}
$grade= ($correct/count($answers))*100;
echo"<p>Score $grade%</p>";
}
但是,当我选择一些答案并单击“提交”按钮时,新脚本会生成一些错误消息,从此开始:
未定义的变量:num
这是产生错误的代码:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$qa = 'q'.$num;
它对应于旧脚本中的这一行:
$question = 'q'.$num;
它看起来像一个简单的修复,但我很困惑。我尝试了很多技巧,但没有任何效果。
我回复了var_dump,两个脚本的结果相同:
var_dump: array(5) {
["q2"]=> string(0) ""
["q9"]=> string(1) "B"
["q10"]=> array(3) {
[0]=> string(1) "A"
[1]=> string(1) "B"
[2]=>
string(1) "C"
}
["PreviousURL"]=>
string(25) "http://g1/test/gw-intro-1"
["user_token"]=>
string(13) "54ee6aac475ac"
}
有谁能告诉我如何让$ num在我的新脚本中接受正确的值?或者我的新脚本是否需要完全更改? (如果您希望我发布一些HTML,请告诉我。)
注意:我编辑了原始帖子以包含一些HTML,因此您可以看到q $ num的值看起来像......
<li id="q9">
<div class="Question">Scientists believe the universe is:</div>
<div class="Answer">
<label class="Wide" for="q9-A"><div class="Radio"><input type="radio" name="q9" id="q9-A" value="A" style="display: none;"> A. disappearing</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-B"><div class="Radio"><input type="radio" name="q9" id="q9-B" value="B" style="display: none;"> B. expanding</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-C"><div class="Radio"><input type="radio" name="q9" id="q9-C" value="C" style="display: none;"> C. contracting</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-D"><div class="Radio"><input type="radio" name="q9" id="q9-D" value="D" style="display: none;"> D. becoming bipolar</div></label></div>
</li>
<li id="q10">
<div class="Question">Check each item that can be found in our solar system.</div>
<div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;">
<label for="q10-A"><input type="checkbox" name="q10[]" id="q10-A" value="A">planet</label>
<label for="q10-B"><input type="checkbox" name="q10[]" id="q10-B" value="B">asteroid</label>
<label for="q10-C"><input type="checkbox" name="q10[]" id="q10-C" value="C">comet</label>
<label for="q10-D"><input type="checkbox" name="q10[]" id="q10-D" value="D">black hole</label>
<label for="q10-E"><input type="checkbox" name="q10[]" id="q10-E" value="E">neutrino star</label>
<label for="q10-F"><input type="checkbox" name="q10[]" id="q10-F" value="F">quasar</label>
</div>
</li>
这就是用于q $ num的PHP:
<label for="q'.$QID.'-'.$Value.'">
...其中$ QID(问题ID)=数字,$ Value =大写字母。