我在这里有一个 phpfiddle:http: //phpfiddle.org/main/code/get-rps
我遇到的问题是,在该Total Marks
列中,它仅显示所有问题的所有行中最后一个问题的总分,而不是遍历所有总分并显示每个问题的正确总分。我在代码中做错了什么?
下面是代码:
<?php
$incorrect_ans = array(
array('A','C','D'),
array('B','C','D'),
array('A','B','D'),
array('A','B','C'));
$searchQuestionNo = array(
1,
2,
2,
3);
$totalMarks = array(
3,
5,
5,
2);
$ques_ans = array(); //to store incorrect answers against ques no.
$q_occ_count = array_count_values($searchQuestionNo);
foreach($searchQuestionNo as $key => $questionNo)
{
if ( ! array_key_exists($questionNo, $ques_ans))
{
if($q_occ_count[$questionNo] === 1) //if a ques has only one correct ans
{
$ques_ans[$questionNo] = $incorrect_ans[$key]; //store the array of incorrect ans against the ques no as key
}
else //if a ques has more than 1 correct ans
{
//find the intersection of incorrect_ans arrays for this ques
$q_keys = array_keys($searchQuestionNo, $questionNo);
$q_incorrect_ans = $incorrect_ans[$q_keys[0]];
foreach($q_keys as $q_key) {
$q_incorrect_ans = array_values(array_intersect($q_incorrect_ans, $incorrect_ans[$q_key]));
}
$ques_ans[$questionNo] = $q_incorrect_ans; //store the array of incorrect ans against the ques no as key
}
}
}
?>
<table border='1' id='penaltytbl'>
<thead>
<tr>
<th class='questionnoth'>Question No.</th>
<th class='answerth'>Incorrect Answer</th>
<th class='marksth'>Total Marks</th>
</tr>
</thead>
<tbody>
<?php
foreach($ques_ans as $questionNo => $inc_ans)
{
$q_row_span = count($inc_ans);
$row_count = 0;
?>
<tr class="questiontd">
<!-- Question No -->
<td class="questionnumtd q<?php echo $questionNo?>_qnum" rowspan="<?php echo $q_row_span ?>">
<?php echo $questionNo?><input type="hidden" name="numQuestion" value="<?php echo $questionNo?>" />
</td>
<!-- first incorrect ans -->
<td class="answertd"><?php echo $inc_ans[$row_count]; ?></td>
<!-- Marks -->
<td class="totalmarkstd" rowspan="<?php echo$q_row_span?>"><?php echo$totalMarks[$key]?></td>
</tr>
<?php
//remaining incorrect answers in separate row (if any) follows here
if($row_count < $q_row_span - 1)
{
for($i=($row_count + 1); $i<$q_row_span; $i++) { ?>
<tr><td class="answertd"><?php echo $inc_ans[$i]; ?></td></tr>
<?php
}
}
}
?>
</tbody>
</table>