我有这张表
我的目标是找个像这样的表
该表有8行3列
我已经存储了这些问题,但我很难以这种格式打印它。
其中在打印4个问题之后,它将转到另一个单元格并再次打印4个问题,然后一旦已经有3个列,它将创建一个新行并再次执行相同的步骤。(8行和3列)< / p>
问题按顺序编号排序。 [在下表中,前4个问题是1,2,3,4然后右边的单元格是6,7,8,9等等......]
我已经坚持了将近8个小时......
这是我目前的代码:
select = "SELECT q.QuestionID, q.QuestionText, q.m, q.l, q.GroupNo, q.SequenceNo FROM question2 q Order By q.SequenceNo";
$result = mysql_query($select);
$question_id = "";
$question_text = "";
$question_m = "";
$question_l = "";
$question_group = "";
//$table = "<table border='1' cellspacing='1'>";
$count_row = 0;
$array = array();
//$count_column = 0;
while($row = mysql_fetch_array($result))
{
$question_id = $row['QuestionID'];
$question_text = $row['QuestionText'];
$question_m = $row['m'];
$question_l = $row['l'];
$question_group = $row['GroupNo'];
$question_sequence = $row['SequenceNo'];
if($count_row > 2)
{
$array[] .= "</div>";
}
$array[] .= "<div style='border=1px';'>";
$array[] .= $question_text ."<br/>";
$count_row++;
}
$table .= "</table>";
echo $table;
先生/女士,你的答案会有很大的帮助。谢谢++
答案 0 :(得分:1)
$querstions = array();
$query = "SELECT QuestionID, QuestionText, m, l, GroupNo, SequenceNo FROM question2 ORDER BY SequenceNo";
$resource = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$querstions[$row['QuestionID']] = $row;
}
/* print table head */
echo "<table><thead>....
/* split up question in master_rows with 12 question each */
foreach(array_chunk($querstions, 12) as $master_row)
{
echo ...
foreach(range(0, 3) as $row_nr)
{
echo ... $master_row[$row_nr] ...;
echo ... $master_row[$row_nr + 4] ...;
echo ... $master_row[$row_nr + 8] ...;
}
echo ...
}
答案 1 :(得分:1)
我不确定我完全理解这个问题,但你可以尝试一下。我相信它会为你提供你想要的输出。
$row_count = 0;
echo "<table border=1>";
while($row = mysql_fetch_array($result)) {
if($row_count%12 == 0) {
echo "<tr>";
}
if($row_count%4 == 0) {
echo "<td>";
}
echo $row['QuestionText']."<br>";
if($row_count%4 == 3) {
echo "</td>";
}
if($row_count%12 == 11) {
echo "</tr>";
}
$row_count++;
}
echo "</table>";
它应该给你一个如下所示的输出:
<table border=1>
<tr>
<td>0<br>1<br>2<br>3<br></td>
<td>4<br>5<br>6<br>7<br></td>
<td>8<br>9<br>10<br>11<br></td>
</tr>
<tr>
<td>12<br>13<br>14<br>15<br></td>
<td>16<br>17<br>18<br>19<br></td>
<td>20<br>21<br>22<br>23<br></td>
</tr>
<tr>
<td>24<br>25<br>26<br>27<br></td>
<td>28<br>29<br>30<br>31<br></td>
<td>32<br>33<br>34<br>35<br></td>
</tr>
...
</table>