我有一个PHP脚本和MSSQL表,我在$right_answer
中存储的变量和$user_answer_select
中用户选择的答案中得到了答案键。格式是这样的
5+10?
A) 10
B) 15
C) 20
D) 25
E) 50
Answer key: B
我想要做的是在B旁边放一个复选标记,如果它是正确的,如果是错误则放一个X,我如何在这里制作if else语句?
这是我目前的代码
if(($user_answer_select == $right_answer) && $user_answer_select == 'a') $a_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'b') $b_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'c') $c_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'd') $d_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'e') $e_sel = "<img src=\"tick_icon.gif\">";
这是错误的,因为一些没有答案的问题会突出显示为true。有什么办法呢?
答案 0 :(得分:1)
if ( $user_answer_select == $right_answer ) {
$correct = true;
} else {
$correct = false;
}
然后在表格上的正确答案中:
<?php echo $correct == true ? 'x' : ''; ?>
答案 1 :(得分:1)
$answers = array ( "A"=>10, "B"=>15, "C"=>20, "D"=>25, "E"=>50 );
$right_answer = "B";
$user_selected_answer = "A";
echo "5+10?<br/>";
foreach ($answers as $key => $value) {
echo $key.") ".$value;
if ($value === $user_selected_answer) {
if ($value === $right_answer){ echo "check!"; }
else { echo "X"; }
}
echo "<br/>";
}
echo "Answer key: $right_answer";