2013-03-08 26 views
1

我試圖確定正確回答的問題並正確回答問題。如果回答正確,則顯示「完全正確」,如果不正確則顯示「不正確」信息。問題無論是否正確,總是顯示該問題的答案不正確。它只顯示一條消息,而不是選擇正確的消息來顯示

下面是代碼:

$check = true; 

    foreach ($studentData['questions'] as $questionId => $questionData) { 

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


if($check) 
{ 
    echo '<p class="green"><strong>Fully Correct</strong></p>'; 
} 
else 
{ 
    echo '<p class="red"><strong>Not Correct/Not Fully Correct</strong></p>'; 
} 

} 
+0

定義你的'$檢查後= TRUE;'你的foreach循環中。 – Rikesh 2013-03-08 06:02:15

回答

1

此行再次設置爲true$check

if($questionData['answer'] == $questionData['studentanswer']) 
{ 
echo '<td width="30%" class="studentanswer green"><strong>'.htmlspecialchars($questionData['studentanswer']).'</strong></td>' . PHP_EOL; 
$check = true; 
} else 
+0

噢,以爲我認爲我不需要這麼做,所以認爲是因爲我宣佈了它。我仍然需要在foreach循環之上進行$檢查或刪除它嗎? – Manixman 2013-03-08 06:05:24

+0

@Manixman不會在'foreach'之上移除它,而且會在if條件中設置爲true。 – 2013-03-08 06:06:38

+1

很酷:)謝謝你 – Manixman 2013-03-08 06:17:19