【问题标题】:How to match student's answer to actual answer如何将学生的答案与实际答案相匹配
【发布时间】: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


【解决方案1】:

试试这个:

$check = true;

foreach ($studentData['questions'] as $questionId => $questionData) {
    $studentAnswer = htmlspecialchars($questionData['studentanswer']);
    $answer = htmlspecialchars($questionData['answer']);
...

        echo '<td width="30%" class="answers">'.htmlspecialchars($questionData['answer']).'</td>' . PHP_EOL;

...
    if($answer == $studentAnswer)
    {
        echo '<td width="30%" class="studentanswer greenAnswer">'.htmlspecialchars($questionData['studentanswer']).'</td>' . PHP_EOL;
    }
    else
    {
        echo '<td width="30%" class="studentanswer redAnswer">'.htmlspecialchars($questionData['studentanswer']).'</td>' . PHP_EOL;
        $check = false;
    }

}

if($check)
{
    echo '<p class="greenAnswer">Fully Correct!</p>';
}
else
{
    echo '<p class="redAnswer">Not Fully Correct!</p>';
}

在您的 CSS 中添加以下内容:

greenAnswer
{
    color:green;
}

redAnswer
{
    color:red;
}

【讨论】:

  • 我尝试稍微更改代码但无法获得文本颜色,请查看我的更新
【解决方案2】:
<?php 
if($questionData['answer'] == $questionData['studentanswer'] {
    $style = 'color:green';
    $checked = 'right';
} else {
    $style = 'color:red';
    $checked = 'not fully correct';
}
?>

...

<td width="30%" class="answers" style="<?php echo $style; ?>">
<?php echo $checked; ?>

【讨论】:

  • 我尝试稍微更改代码但无法获得文本颜色,请查看我的更新
【解决方案3】:

您可以使用条件来实现这一点。

使用 php 的 if() 函数,您可以确保您的脚本在处理某段代码之前满足您选择的某些条件。

因此,要检查学生的答案是否与教师的答案相符,在循环内,您将添加如下内容:

if( $questionData['studentanswer'] == $questionData['answer'] )
{
    // The answer was correct
}
else {
    // This is for the answer not being correct
}

此代码仅显示了如何实现您想要完成的目标的示例,当然您需要自己制作以完全符合您的需求。

如果您有任何其他问题或需要进一步帮助,请随时提出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 2016-03-30
    • 1970-01-01
    相关资源
    最近更新 更多