在while循环中按列显示表数据 [英] Displaying table data column-wise in a while loop

查看:107
本文介绍了在while循环中按列显示表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库中检索所有数据,将其放在一个表中(如果需要,可以有多个表),并以多个页面的4个拆分显示它们。

I'm attempting to retrieve all that data from a database, put it in a table (more than one table if necessary) and display them column-wise in lots of 4 split across multiple pages.

我想知道如何使表格水平显示,例如

I would like to know how I would get the tables to display horizontally e.g.

表格标题 表标题 表标题

表数据     表数据    表数据

表数据      表数据    表数据

Table Header   Table Header  Table Header
Table Data       Table Data      Table Data
Table Data       Table Data      Table Data

而不是:

表标题

表数据

表数据

Table Header
Table Data
Table Data

表头

表数据等

Table Header
Table Data etc.

代码到目前为止:

// 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>


$ b

Any suggestions on how to do this or to point me in the right direction would be greatly appreciated.

推荐答案

任何有关如何执行此操作或指向正确方向的建议都将非常感谢。这里没有足够的信息来说明您是否应该使用表,因为您提供了非数据。

There's not enough information here to say whether or not you should be using a table because you provided non-data.

如果我们正在编写一个壁橱组织应用程序,我们的数据可能会出来这样的数据库:

If we're writing a closet organization application, our data might come out of the database like this:

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

我们将这样写成我们的循环:

We would write our loop something like this:

<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 对于你使用的接口,所以我不能帮助你比这更远。在结果的第一个行上使用输出缓冲,并将收集器变量作为数组收集所有标头可以工作,而不需要重置指针。

The headers? Well, typically I would recommend looping twice and resetting the pointer, but I don't see the equivalent of mysql_data_seek or pg_result_seek for the interface you're using, so I can't help you any farther than this. Using output buffering on the first "row" of results combined with a collector variable gathering up all of the headers as an array can work without the need to reset the pointer.

如果你只是想在4列中输出结果,因为你认为它看起来更漂亮,而不是因为它表示表格数据,使用CSS列是更好的方式。

If you're just wanting to spit out the results in 4 columns because you think it looks prettier and not because it expresses tabular data, using CSS columns would be the better way to go.

这篇关于在while循环中按列显示表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆