如何使用下面的示例正确设置布局 [英] How to correctly set layout using example below
问题描述
问题编号:1正确答案(s) B错误答案ACD
问题编号:2正确答案AC错误答案BD
问题编号:3正确答案D错误答案ABC
下面显示了当前的布局和布局方式:
当前输出的代码是这样的:
< table border ='1'id ='penaltytbl'>
< thead>
< 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?> _qnumrowspan =<?php echo $ row_span [$ questionNo]?>>
<?php echo $ questionNo?>< input type =hiddenname =numQuestionvalue =<?php echo $ questionNo?> />
< / td>
<?php
}
?>
< td class =answertd><?php echo implode(',',$ incorrect_ans [$ key]);?>< / td>
< / tr>
<?php
$ prev_ques = $ questionNo;
}
?>
< / tbody>
< / table>
要将当前表结构更改为所需的表结构你需要修改每个< td class =questionnumtd> / code>和
(2)如何回应您的< td class =answertd>
的
最简单的是< td class =answertd>
。 Change
< td class =answertd><?php echo implode(',',$ incorrect_ans [$键]);?>< / TD>
< / tr>
至
<?php
foreach($ incorrect_ans [$ key] as $ answer){?>
< td class =answertd><?php echo $ answer?>< / td>
< / tr>
<?php
}
< ; td class =questionnumtd>
rowspan
有点难度,因为在你当前的表结构&代码显示问题2有两行 $ incorrect_ans [$ key]
。使用下面的 foreach
循环,它根据所有的 $ incorrect_ans [$ key]
设置一个问题rowspan $ q_row_span [$ i]
p>
$ q_counter = 1; //计数器为$ row_span
$ i = key($ row_span); //获得第一个问题编号
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 ++; }
}
p>
< table border ='1'id ='penaltytbl'>
< thead>
< tr>
< / thead>
< tbody>
<?php
$ row_span = array_count_values($ searchQuestionNo);
$ q_counter = 1; //计数器为$ row_span
$ i = key($ row_span); //获得第一个问题编号
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?> _qnumrowspan =<?php echo $ q_row_span [$ questionNo]?>>
<?php echo $ questionNo?>< input type =hiddenname =numQuestionvalue =<?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
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>
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
这篇关于如何使用下面的示例正确设置布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!