如何循环一组答案并仅显示不正确的答案

时间:2013-01-12 06:12:12

标签: php html

因此,下面是每个问题的正确和错误答案:

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

问题是我正在检索具有多个答案(多个正确答案)的问题的错误答案。在问题只有一个正确答案的情况下检索错误答案没有任何问题,只有多个正确答案。

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

enter image description here

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

因此,它识别A和C是正确的答案,因为它从问题2中显示的第一组错误答案中删除了A,并从问题2中的第二组错误答案中删除了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);
    $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>

2 个答案:

答案 0 :(得分: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>

<强> Demo

即使您有3个正确答案,上面的代码也能正常运行

<强>更新

这里没有添加代码,只做了一些修改 的 updated Demo

答案 1 :(得分: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