所以我遇到了这个问题:
我做了一个在线测验,答案被推入一个名为答案的数组中。它工作正常,但是当我尝试重新显示正确的答案以及用户提交的答案时,我得到了 foreach() 提供的无效参数
有一个 while 循环从数据库和问题中获取正确答案。我需要显示例如以下内容:
问题 1: -------- 正确答案:A.XXX 您的答案:$answer[0]
问题 2: -------- 正确答案:C.XXX 您的答案:$answer[1]
<?php
$con=mysqli_connect("localhost","xxx","xxx");
if(!$con) {
die('Could Not Connect: '.mysqli_error($con));
}
mysqli_select_db($con,"rankone");
$res=mysqli_query($con,"SELECT * FROM rankone_quizzes WHERE (Bundle='Free-Exam-A') ORDER BY ID ASC");
echo '<ol>';
while($row=mysqli_fetch_array($res)) {
foreach ($answer as $answer) {
echo'<li> <h3> '.($row["Question"]).'</h3>';
if (($row["CorrectAnswer"]) == "A"){
echo'<p>A. '.($row["Option1"]).'</p>';
echo'<p>You Answer: $answer</p>';
}elseif(($row["CorrectAnswer"]) == "B") {
echo'<p>B. '.($row["Option2"]).'</p>';
echo'<p>You Answer: $answer</p>';
}elseif(($row["CorrectAnswer"]) == "C") {
echo'<p>C. '.($row["Option3"]).'</p>';
echo'<p>You Answer: $answer</p>';
}
echo'</li>';
echo'<br><br>';
}
echo'</ol>';
?>
GCT1015