1

以下に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するなどの変数が必要です。 100%一致しない場合は、文字列を赤で表示します。$checkfully correctnot 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;
}
4

3 に答える 3

0
<?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; ?>
于 2013-03-08T03:24:05.467 に答える
0

これを達成するために条件を使用できます。

PHP の if() 関数を使用すると、特定のコードを処理する前に、選択した特定の条件をスクリプトが満たしていることを確認できます。

したがって、生徒の答えが教師の答えと一致するかどうかを確認するには、ループ内に次のようなものを追加します。

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

このコードは、達成しようとしていることを達成する方法の例を示しているだけです。もちろん、ニーズに正確に一致するように独自に作成する必要があります。

他に質問がある場合、またはさらにサポートが必要な場合は、お気軽にお問い合わせください。

于 2013-03-08T03:24:48.943 に答える
0

これを試して:

$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;
}
于 2013-03-08T03:30:13.810 に答える