PHP数组与foreach组合计算
在一个测验应用程序中,我使用表单获取用户答案。我正在从数据库表中检索正确答案。我想将正确答案与用户的答案进行比较,并计算有多少答案是正确的,有多少答案是错误的 这是我的表格:PHP数组与foreach组合计算,php,for-loop,foreach,Php,For Loop,Foreach,在一个测验应用程序中,我使用表单获取用户答案。我正在从数据库表中检索正确答案。我想将正确答案与用户的答案进行比较,并计算有多少答案是正确的,有多少答案是错误的 这是我的表格: <form id="question" class="" action="quiz_ans.php" method="post"> <table id="quiz-question" align="center" class="row-border compact order-column str
<form id="question" class="" action="quiz_ans.php" method="post">
<table id="quiz-question" align="center" class="row-border compact order-column stripe">
<input class="form-control" type="hidden" name="NumberofQuestions" id="NumberofQuestions" value="<?php echo $NumberofQuestions; ?>">
<thead>
<?php
if($QuizQuestions) {
$i=1;
foreach($QuizQuestions as $row):
?>
<tr>
<th><?php echo $i; ?>. <?php echo $row->Question; ?>
<br>
<?php if(isset($row->Screenshot)) { ?>
<img src="<?php echo htmlspecialchars($row->Screenshot); ?>" alt="test" height="300" width="980">
<?php } ?>
</th>
</tr>
</thead>
<tbody>
<?php if(isset($row->Option1)) { ?>
<tr class="info">
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="0"><?php echo $row->Option1; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option2)) { ?>
<tr class="info">
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="1"> <?php echo $row->Option2; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option3)) { ?>
<tr>
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="2"> <?php echo $row->Option3; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option4)) { ?>
<tr>
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="3"><?php echo $row->Option4; ?></td>
</tr>
<?php } ?>
<tr>
<td><label for="AnswerReason">Why?</label><input class="form-control" type="text" name="AnswerReason[]" id="AnswerReason" value=""></td>
</tr>
<?php if(isset($row->Id)) { ?>
<tr>
<td><input class="form-control" type="hidden" name="QuestionId[]" id="QuestionId" value="<?php echo $row->Id; ?>"></td>
</tr>
<?php } ?>
</tbody>
<?php
$i++;
endforeach;
}
?>
</table>
<br>
<input type="submit" name="submit" value="Submit" class="btn btn-success">
</form>
我正在从数据库表中检索正确答案:
try {
$sql = "CALL spQuizAnswers(:quiz_num)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':quiz_num', $quiz_num, PDO::PARAM_STR, 50);
$stmt->execute();
$QuizAns=$stmt->fetchAll();
$stmt->closeCursor();
} catch (PDOException $e) {
die("Error occurred:" . $e->getMessage());
}
我正在比较用户的答案和正确答案:
for ($i=0; $i<$NumberofQuestions; $i++) {
if($QuizAns) {
foreach($QuizAns as $row):
if($row->CorrectAns == $ans[$i]){
$right++;
} elseif($ans[$i] == 4){
$not_answered++;
} else {
$wrong++;
}
endforeach;
}
}
$CorrectAnswer = $right;
$WrongAnswer = $wrong;
$NotAnswered = $not_answered;
$TotalQuestion = $right+$wrong+$not_answered;
for($i=0;$iCorrectAns==$ans[$i]){
$right++;
}elseif($ans[$i]==4){
$not_-answered++;
}否则{
$error++;
}
endforeach;
}
}
$CorrectAnswer=$right;
$ErrorAnswer=$Error;
$NotAnswered=$not_response;
$TotalQuestion=$right+$Error+$未回答;
它没有给出正确的计算对于5个问题,它给出$TotalQuestion=25。
如何实现正确的计算?任何帮助都将不胜感激。除非我遗漏了什么,否则您的表单有:
@Loko$NumberofQuestions就是表单中有多少个问题。假设一个测验有5个问题,所以$NumberofQuestions=5。当我从数据库中获取问题时,我得到了它,比如:{$sql=“CALL-spQuizQuestions(:quick_-num)”;$stmt=$pdo->prepare($sql);$stmt->bindParam(':quick_-num',$quick_-num,pdo::PARAM_-STR,50);$stmt->execute();$QuizQuestions=$stmt->fetchAll();$NumberofQuestions=$stmt->->rowCount();$stm->(PDOException$e){die(“发生错误:”..e->getMessage());}如果有5个问题,那么查询和for
循环5次,这5次循环中的每一次循环5次。您需要匹配ID
@Abracadver如何匹配ID?
for ($i=0; $i<$NumberofQuestions; $i++) {
if($QuizAns) {
foreach($QuizAns as $row):
if($row->CorrectAns == $ans[$i]){
$right++;
} elseif($ans[$i] == 4){
$not_answered++;
} else {
$wrong++;
}
endforeach;
}
}
$CorrectAnswer = $right;
$WrongAnswer = $wrong;
$NotAnswered = $not_answered;
$TotalQuestion = $right+$wrong+$not_answered;