我正在尝试从数据库中检索所有数据,将其放在一个表中(如果需要,可以放在多个表中),并在多个页面中以4个分组的方式逐列显示它们。
我想知道如何让表格水平显示,例如。
表标题表标题表标题
表数据表数据表数据
表数据表数据表数据
而不是:
表头
表数据
表数据
表头
表数据等。
以下是目前的代码:
// While loop that will display the results in groups of 4
while($row=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{ $newDate = $row['datenow']->format('d/m/Y'); ?>
<table id="syncresults">
<thead>
<tr>
<th scope="col" id="dateheader"> <?php echo $newDate ?></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['nbsactive']; ?></td>
</tr>
<tr>
<td><?php echo $row['nbsmanysynced']; ?></td>
</tr>
<tr>
<td><?php echo $row['nbsthreedays']; ?></td>
</tr>
</tbody>
对于如何做到这一点或指出我正确方向的任何建议将不胜感激。
答案 0 :(得分:0)
在表格中使用表格。
// php fetch data
while($row=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
echo'<table>
<tr><td><table> place data here</table></td></tr>
<tr><td><table>or here </table></td></tr>
<tr><td><table>or here, depending on what the data is</table></td></tr>
</table>';
}
答案 1 :(得分:0)
将您的代码替换为:
<table>
<?php
$i=0;
while($row=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{ $newDate = $row['datenow']->format('d/m/Y'); ?>
$j=$i%3;
?>
<?php
if($j==0)
{
echo"<tr>";
}
?>
<td>
<table id="syncresults">
<thead>
<tr>
<th scope="col" id="dateheader"> <?php echo $newDate ?></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['nbsactive']; ?></td>
</tr>
<tr>
<td><?php echo $row['nbsmanysynced']; ?></td>
</tr>
<tr>
<td><?php echo $row['nbsthreedays']; ?></td>
</tr>
</tbody></table>
</td>
<?php
if($j==2)
{
echo"</tr><tr><td colspan='3'> </td></tr>";
}
$i++;
}
?>
</table>
答案 2 :(得分:0)
这里没有足够的信息来说明你是否应该使用表格,因为你提供了非数据。
如果我们正在编写壁橱组织应用程序,我们的数据可能会出现在数据库中,如下所示:
John | Shoes | 3
John | Pants | 10
Sally | Shoes | 12
Sally | Pants | 8
Billy | Shoes | 4
Billy | Pants | 9
Kate | Shoes | 6
Kate | Pants | 6
但我们希望像这样显示:
Member | Shoes | Pants
John | 3 | 10
Sally | 12 | 8
Billy | 4 | 9
Kate | 6 | 6
我们会写这样的循环:
<table>
<tr>
<?
$last = null;
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
if ($last != $row['member']) {
if ($last) { // $last is null on our first pass through the loop, so don't print the tr's
?>
</tr>
<tr>
<?
}
?>
<td><?=$row['member']?></td>
<?
$last = $row['member'];
}
?>
<td><?=$row['quantity']?></td>
<?
}
?>
</tr>
</table>
标题?好吧,通常我会建议循环两次并重置指针,但我没有看到你正在使用的接口相当于mysql_data_seek
或pg_result_seek
,所以我无法再帮助你了比这个。在结果的第一个“行”上使用输出缓冲与收集器变量相结合,收集所有标头作为数组可以在不需要重置指针的情况下工作。
如果您只是想在4列中吐出结果,因为您认为它看起来更漂亮而不是因为它表示表格数据,那么使用CSS列将是更好的方法。
答案 3 :(得分:0)
$th1="<tr>";
$td1="<tr>";
$td2="<tr>";
$td3="<tr>";
while($row=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
$newDate = $row['datenow']->format('d/m/Y');
$th1.="<th scope='col' id='dateheader'>".$newDate."</th>";
$td1.="<td>".$row['nbsactive']."</td>";
$td2.="<td>".$row['nbsmanysynced']."</td>";
$td3.="<td>".$row['nbsthreedays']."</td>";
}
$th1.="</tr>";
$td1.="</tr>";
$td2.="</tr>";
$td3.="</tr>";
$result="<table id='syncresults'>".$th1.$td1.$td2.$td3."</table>";