Reputation: 859
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(s) A C Incorrect Answers B D
Question Number: 3 Correct Answer(s) D Incorrect Answers A B C
Below shows the current layout and the way it should be laid out:
The code for the current output is this:
<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>
Upvotes: 2
Views: 142
Reputation: 2534
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($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>
the code above works even if you are having 3 correct answers
Upvotes: 0
Reputation: 12433
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
<td class="answertd"><?php echo implode(',', $incorrect_ans[$key]);?></td>
</tr>
to
<?php
foreach($incorrect_ans[$key] as $answer){ ?>
<td class="answertd"><?php echo$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 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++; }
}
Try -
<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>
See this phpfiddle that shows your original structure, and the new structure with the code above. http://phpfiddle.org/main/code/z8e-74b
Upvotes: 1