2

所以让我们说下面是每个问题的正确和错误答案:

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>
4

2 回答 2

1

要将您当前的表结构更改为所需的表结构,您需要修改 -
(1)rowspan在每个您的<td class="questionnumtd">
(2) 您如何回显您<td class="answertd">

最简单的是<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]

$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++; }
}

尝试 -

<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>

请参阅显示您的原始结构的这个 phpfiddle,以及带有上面代码的新结构。http://phpfiddle.org/main/code/z8e-74b

于 2013-01-11T04:01:18.727 回答
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>

演示

即使您有 3 个正确答案,上面的代码也有效

于 2013-01-12T05:50:43.263 回答