Skip to content Skip to sidebar Skip to footer

How To Correctly Set Layout Using Example Below

So lets say below are the correct and incorrect answers for each question: Question Number: 1 Correct Answer(s) B Incorrect Answers A C D Question Number: 2 Correct Answer(

Solution 1:

To change your current table structure to your desired table structure you need to modify- (1) the rowspan in each your <td class="questionnumtd"> and (2) how you echo your <td class="answertd">'s

The easiest is the <td class="answertd">. Change

<tdclass="answertd"><?phpecho implode(',', $incorrect_ans[$key]);?></td></tr>

to

<?phpforeach($incorrect_ans[$key] as$answer){ ?><tdclass="answertd"><?phpecho$answer?></td></tr><?php
    }

The <td class="questionnumtd">rowspan is a little harder as in your current table structure & code you show that there is 2 rows of $incorrect_ans[$key] for question 2. Using the foreach loop below, it sets a question rowspan $q_row_span[$i] based off all the $incorrect_ans[$key]

$q_counter = 1;// counter for $row_span$i = key($row_span);  // gets first question numberforeach ($incorrect_ansas$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++; }
}

Try -

<tableborder='1'id='penaltytbl'><thead><tr><thclass='questionth'>Question No.</th><thclass='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 numberforeach ($incorrect_ansas$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($searchQuestionNoas$key=>$questionNo){

?><trclass="questiontd"><?phpif($questionNo != $prev_ques){
    ?><tdclass="questionnumtd q<?phpecho$questionNo?>_qnum"rowspan="<?phpecho$q_row_span[$questionNo]?>"><?phpecho$questionNo?><inputtype="hidden"name="numQuestion"value="<?phpecho$questionNo?>" /></td><?php
    }  

    foreach($incorrect_ans[$key] as$answer){ ?><tdclass="answertd"><?phpecho$answer?></td></tr><?php
    }
$prev_ques = $questionNo;
}
?></tbody></table>

See this phpfiddle that shows your original structure, and the new structure with the code above. http://phpfiddle.org/main/code/z8e-74b

Solution 2:

here is exactly what you want your output to be like

<?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($searchQuestionNoas$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_keysas$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);
?><tableborder='1'id='penaltytbl'><thead><tr><thclass='questionth'>Question No.</th><thclass='answerth'>Incorrect Answer</th></tr></thead><tbody><?phpforeach($ques_ansas$questionNo => $inc_ans)
{
    $q_row_span = count($inc_ans);
    ?><trclass="questiontd"><tdclass="questionnumtd q<?phpecho$questionNo?>_qnum"rowspan="<?phpecho$q_row_span?>"><?phpecho$questionNo?><inputtype="hidden"name="numQuestion"value="<?phpecho$questionNo?>" /></td><?phpforeach ($inc_ansas$ans)
        {
        ?><tdclass="answertd"><?phpecho$ans; ?></td></tr><?php
        }
}
?></tbody></table>

Demo

the code above works even if you are having 3 correct answers

Post a Comment for "How To Correctly Set Layout Using Example Below"