2

I have a phpfiddle here: http://phpfiddle.org/main/code/get-rps

The problem I am getting is that in the Total Marks column, it is only display the total marks for the last question in all of the rows for all questions, rather than looping through all the total marks and displaying the correct total marks for each question. What am I doing wrong in the code?

Below is code:

<?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);

$totalMarks = array(
                   3,
                   5,
                   5,
                   2);

$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_values(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
        }
    }
}
?>
<table border='1' id='penaltytbl'>
<thead>
<tr>
<th class='questionnoth'>Question No.</th>
<th class='answerth'>Incorrect Answer</th>
<th class='marksth'>Total Marks</th>
</tr>
</thead>
<tbody>
<?php

foreach($ques_ans as $questionNo => $inc_ans)
{
    $q_row_span = count($inc_ans);
    $row_count = 0;
    ?>
    <tr class="questiontd">

        <!-- Question No -->
        <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>

        <!-- first incorrect ans -->
        <td class="answertd"><?php echo $inc_ans[$row_count]; ?></td>

        <!-- Marks -->
       <td class="totalmarkstd" rowspan="<?php echo$q_row_span?>"><?php echo$totalMarks[$key]?></td>
    </tr>
    <?php
        //remaining incorrect answers in separate row (if any) follows here
    if($row_count < $q_row_span - 1) 
    {
        for($i=($row_count + 1); $i<$q_row_span; $i++) { ?>     
            <tr><td class="answertd"><?php echo $inc_ans[$i]; ?></td></tr>
    <?php
        }
    }
}
?>
</tbody>
</table>
4

1 に答える 1

0

この行はあなたの問題です。

<?php echo$totalMarks[$key]?>

$ keyは上のループから設定され、下のループではリセットされないため、そのループの最後の値が保持されます。私はそれがすべきだと思います<?php echo$totalMarks[$questionNo]?>

于 2013-01-12T17:12:13.823 に答える