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
    }

現在のテーブル構造とコードでは、質問 2 の行が 2 つあることが示されているため、これは少し難しくなります。以下のループを使用して、すべて<td class="questionnumtd"> rowspanに基づいて質問の行スパンを設定します。$incorrect_ans[$key]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 に答える