【发布时间】:2013-03-08 03:15:18
【问题描述】:
我在下面有一个 foreach 循环,它显示每个表格行的数据:
foreach ($studentData['questions'] as $questionId => $questionData) {
...
echo '<td width="30%" class="answers">'.htmlspecialchars($questionData['answer']).'</td>' . PHP_EOL;
...
echo '<td width="30%" class="studentanswer">'.htmlspecialchars($questionData['studentanswer']).'</td>' . PHP_EOL;
}
我想要做的是,如果 studentanswer 匹配 answer,那么 studentanswer 将变为绿色,如果不匹配,则以红色显示错误答案,如果完整 studentanswer 匹配 100%使用answer,那么我想要一个变量,例如$check,以绿色显示字符串站fully correct,否则如果不是100% 匹配,则以红色显示字符串not full correct。
所以例如上面的代码可以显示这个:
Answer: B C
Student Answer: B D
上述输出应将学生答案B 显示为绿色,因为它与答案B 匹配,但学生答案D 应为红色,因为答案中没有D。变量$check 应以红色表示not fully correct,因为学生的答案并不完全正确,只是部分正确。
但是如何实现呢?
更新:
它没有改变文本的颜色:
if($questionData['answer'] == $questionData['studentanswer']) {
$style = 'green';
$checked = 'Fully Correct';
} else {
$style = 'red';
$checked = 'Not Correct / Not Fully Correct';
}
echo '<td width="30%" class="answers '.$style.'">'.htmlspecialchars($questionData['answer']).'</td>' . PHP_EOL;
...
echo '<td width="30%" class="studentanswer '.$style.'">'.htmlspecialchars($questionData['studentanswer']).'</td>' . PHP_EOL;
CSS:
.red{
color: red;
}
.green{
color: green;
}
【问题讨论】:
-
不要将 $style 放在单引号内。这样做
'.$style.' -
@PriestVallon 我将 `class="$style"` 更改为 `class=".$style."` 但仍然没有颜色
-
类不应被声明两次。你应该有类似
class="a b"的东西,其中 a 和 b 是单独的类。 -
@PriestVallon 我不太明白,这是否意味着我需要将顶部的
$style更改为不同的变量名? -
我把你更新的代码改成了我认为应该的样子!
标签: php