0

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

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

问题是我为具有多个答案(多个正确答案)的问题检索错误答案的方式。检索一个问题只有一个正确答案的错误答案没有任何问题,只有多个正确答案。

下面显示了错误答案的当前显示方式及其外观(我在下面进行了颜色编码,以便您查看问题所在):

在此处输入图像描述

如果一个问题中有多个正确答案,它似乎在该问题中所做的只是循环遍历所有答案,删除一个正确答案并显示其余答案,然后再次循环遍历所有答案并删除另一个正确答案并显示当前答案的其余部分。

因此它识别出 A 和 C 是正确答案,因为它A从问题 2 中显示的第一组错误答案中删除,并C从问题 2 中的第二组错误答案中删除。但就像我说的那样,这是不正确的显示,它应该只显示每个问题的每一行都有一个不正确的答案。

我的问题是如何固定显示器以匹配它的外观?

当前输出的代码是这样的:

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

下面的代码显示了它如何检索每个问题的错误答案,然后将其存储在数组中。这段代码显示在上面的 HTML 表格上方:

$specialOptionTypes = array('Yes or No' => array( 'Yes', 'No' ),'True or False' => array( 'True', 'False' ));

while ($stmt->fetch()) {

// Do this for each row:
if ( array_key_exists( $dbOptionType, $specialOptionTypes ) ) {
    $options = $specialOptionTypes[$dbOptionType];
} else if ( preg_match( '/^([A-Z])-([A-Z])$/', $dbOptionType, $match ) ) {
    $options = range( $match[1], $match[2] );
} else {
    // issue warning about unrecognized option type
    $options = array();
}
$right = str_split( $dbAnswer ); 
$wrong = array_diff( $options, $right );  

        $searchQuestionNo[] = $dbQuestionNo;
        $incorrect_ans[] = $wrong;
      } 

更新:

如果一Incorrect Answer列又一列会发生什么情况,下面的代码会弄乱布局:

<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>
          <?php  
            }
          ?>         
            <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>
        </tr>
        <?php
}
?>
</tbody>
4

2 回答 2

1

这正是您希望输出的样子

<?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 个正确答案,上面的代码也有效

更新

这里不加代码,只是做了一些修改 更新了Demo

于 2013-01-12T07:34:43.067 回答
0

这不起作用吗?

foreach($searchQuestionNo as $key => $qNum)
{
    $answers = $incorrect_ans[$key];
    $aCount = count($answers);
    $i = 0;
    foreach($answers as $answer)
    {
        echo '<tr>';
        if($i == 0)
        {
           echo "<td class='questionnumtd q{$qNum}_qnum' rowspan='$aCount'>$qNum";
           echo "<input type='hidden' name='numQuestion' value='$qNum' />";
           echo "</td>";
        }
        echo "<td class='answertd'>$answer</td>";
        echo "</tr>";
        $i++;
    }
}

示例:http: //ideone.com/7asOjm

于 2013-01-12T06:36:53.863 回答