$arrQuestionContent = array();
$arrAnswerFile = array();
while ($qandaqrystmt->fetch()) {
$arrQuestionId[] = $qandaQuestionId;
$arrQuestionContent[] = $qandaQuestionContent;
}
...
while ($imgqrystmt->fetch()) {
$arrAnswer[] = $AnswerFile;
}
上面我有两个从db获取数据的数组,arrQuestionContent[]
获取所有问题,$arrAnswer[]
获取每个问题的所有答案。
但有些问题可能不包含任何答案,导致我的表格中的每个问题行Notice: Undefined offset:
都没有答案。
我的问题是,如果问题行不包含答案,我该如何让表格在Answer
列下显示空白行?
此外,问题可能有多个答案。如果是这种情况,那么我希望具有多个答案的问题行能够在问题行的Answer
列下包含其所有答案。目前它只显示一个问题行的答案,假设有多个答案:
下表:
<table id="tableqanda" align="center" cellpadding="0" cellspacing="0" border="1">
<thead>
<tr>
<th width="30%" class="question">Question</th>
<th width="12%" class="images">Answer</th>
</tr>
</thead>
<tbody>
<?php
foreach ($arrQuestionId as $key=>$question) {
echo '<tr class="tableqandarow">'.PHP_EOL;
echo '<td width="30%" class="question">'.htmlspecialchars($arrQuestionContent[$key]).'</td>' . PHP_EOL;
echo '<td width="12%" class="images">'. ( empty ($arrAnswerFile[$key]) ) ? " " : htmlspecialchars( is_array( $arrAnswerFile[$key] ) ? implode(",", $arrAnswerFile[$key]) : $arrAnswerFile[$key] ) . '</td>' . PHP_EOL;
echo '</tr>'.PHP_EOL;
}
?>
</tbody>
</table>
更新
屏幕截图显示了应该是什么样的表:
更新2:
var_dump($arrQuestionContent);
的结果:
array(2) { [0]=> string(19) "Name these 2 things" [1]=> string(11) "What is 4+4" }
var_dump($arrAnswerFile);
的结果:
array(3) {
[0]=> string(1) "A"
[1]=> string(1) "C"
[2]=> string(1) "A"
}
下面是表格截图。无论问题是否有答案,都会发生这种情况:
数据库:
QuestionId QuestionContent Answer
1 Name these 2 Things A
1 Name these 2 Things C
2 What is 4+4? A
答案 0 :(得分:1)
为了显示空单元格,您可以执行以下操作:
echo '<td>'. ( empty ($arrAnswerFile[$key]) ) ? " " : htmlspecialchars( $arrAnswerFile[$key] ) . '</td>' . PHP_EOL;
对于其他情况,要显示多个答案,如何做商店主题? 如果您的$ arrAnswerFile [$ key]变量是一个数组,您可以执行以下操作来解决它:
echo '<td>'. ( ( empty ($arrAnswerFile[$key]) ) ? " " : htmlspecialchars( is_array( $arrAnswerFile[$key] ) ? implode(",", $arrAnswerFile[$key]) : $arrAnswerFile[$key] ) ). '</td>' . PHP_EOL;
并且答案将以逗号分隔显示,但如果您以其他方式存储答案,请告诉我们我们可以提出更好的解决方案
答案 1 :(得分:1)
在我们的聊天讨论之后,我们已经解决了问题的一部分,即为其问题分配每个答案,对于其余的,多个答案的情况,请使用以下代码:
while ($qandaqrystmt->fetch()) {
$arrQuestionId[ $qandaQuestionId ] = $qandaQuestionId;
$arrQuestionNo[ $qandaQuestionId ] = $qandaQuestionNo;
$arrQuestionContent[ $qandaQuestionId ] = $qandaQuestionContent;
$arrOptionType[ $qandaQuestionId ] = $qandaOptionType;
}
...
while ($imgqrystmt->fetch()) {
$arrImageFile[ $imgQuestionId ][] = basename($imgImageFile);
}
然后使用以下内容显示HTML表格中的元素:
foreach ($arrQuestionId as $key=>$question) {
echo '<tr class="tableqandarow">'.PHP_EOL;
echo '<td width="5%" class="questionno">'.htmlspecialchars($arrQuestionNo[$key]).'</td>' . PHP_EOL;
echo '<td width="27%" class="question">'.htmlspecialchars($arrQuestionContent[$key]).'</td>' . PHP_EOL;
echo '<td width="7%" class="option">'.htmlspecialchars($arrOptionType[$key]).'</td>' . PHP_EOL;
echo '<td width="11%" class="imagetd">'. ( ( empty ($arrImageFile[$key]) ) ? " " : '<ul class="qandaul"><li>'.htmlspecialchars( is_array( $arrImageFile[$key] ) ? implode(" ", $arrImageFile[$key]) : $arrImageFile[$key] ) ). '</li></ul></td>' . PHP_EOL;
echo '</tr>'.PHP_EOL;
}
我希望现在有效,如果不是只告诉我它给出了什么
答案 2 :(得分:0)
不清楚如何设置变量$ qandaQuestionContent和$ AnswerFile(获取值)。
也许您应该修改问题中提到的两个 while 循环来获取$ qandaQuestionContent和$ AnswerFile的值。