因此,下面是每个问题的正确和错误答案:
Question Number: 1 Correct Answer(s) B Incorrect Answers A C D
Question Number: 2 Correct Answer(s) A C Incorrect Answers B D
Question Number: 3 Correct Answer(s) D Incorrect Answers A B C
下面显示了当前的布局及其布局方式:
当前输出的代码是:
<table border='1' id='penaltytbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='answerth'>Incorrect Answer</th></tr>
</thead>
<tbody>
<?php
$row_span = array_count_values($searchQuestionNo);
$prev_ques = '';
foreach($searchQuestionNo as $key=>$questionNo){
?>
<tr class="questiontd">
<?php
if($questionNo != $prev_ques){
?>
<td class="questionnumtd q<?php echo$questionNo?>_qnum" rowspan="<?php echo$row_span[$questionNo]?>">
<?php echo$questionNo?><input type="hidden" name="numQuestion" value="<?php echo$questionNo?>" />
</td>
<?php
}
?>
<td class="answertd"><?php echo implode(',', $incorrect_ans[$key]);?></td>
</tr>
<?php
$prev_ques = $questionNo;
}
?>
</tbody>
</table>
答案 0 :(得分:1)
要将当前表格结构更改为所需的表格结构,您需要修改 -
(1)rowspan
和/ <td class="questionnumtd">
中的<td class="answertd">
(2)你如何回应<td class="answertd">
的
最简单的是 <td class="answertd"><?php echo implode(',', $incorrect_ans[$key]);?></td>
</tr>
。改变
<?php
foreach($incorrect_ans[$key] as $answer){ ?>
<td class="answertd"><?php echo$answer?></td>
</tr>
<?php
}
到
<td class="questionnumtd">
rowspan
$incorrect_ans[$key]
有点困难,就像你目前的表格结构一样。代码显示问题2有2行foreach
。使用下面的$q_row_span[$i]
循环,它根据所有$incorrect_ans[$key]
<table border='1' id='penaltytbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='answerth'>Incorrect Answer</th></tr>
</thead>
<tbody>
<?php
$row_span = array_count_values($searchQuestionNo);
$q_counter = 1;// counter for $row_span
$i = key($row_span); // gets first question number
foreach ($incorrect_ans as $key => $val){
if($q_counter == 1){
$q_row_span[$i] = count($val);}
else{
$q_row_span[$i] += count($val);}
if($q_counter >= $row_span[$i]){
$q_counter = 1;
$i++;}
else{
$q_counter++; }
}
$prev_ques = '';
foreach($searchQuestionNo as $key=>$questionNo){
?>
<tr class="questiontd">
<?php
if($questionNo != $prev_ques){
?>
<td class="questionnumtd q<?php echo$questionNo?>_qnum" rowspan="<?php echo$q_row_span[$questionNo]?>">
<?php echo$questionNo?><input type="hidden" name="numQuestion" value="<?php echo$questionNo?>" />
</td>
<?php
}
foreach($incorrect_ans[$key] as $answer){ ?>
<td class="answertd"><?php echo$answer?></td>
</tr>
<?php
}
$prev_ques = $questionNo;
}
?>
</tbody>
</table>
尝试 -
{{1}}
请参阅此phpfiddle,其中显示了您的原始结构,以及带有上述代码的新结构。 http://phpfiddle.org/main/code/z8e-74b
答案 1 :(得分:0)
这正是您想要的输出
<?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);
$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_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
}
}
}
var_dump($ques_ans);
?>
<table border='1' id='penaltytbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='answerth'>Incorrect Answer</th></tr>
</thead>
<tbody>
<?php
foreach($ques_ans as $questionNo => $inc_ans)
{
$q_row_span = count($inc_ans);
?>
<tr class="questiontd">
<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>
<?php
foreach ($inc_ans as $ans)
{
?>
<td class="answertd"><?php echo $ans; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<强> Demo 强>
即使您有3个正确答案,上面的代码也能正常运行