-1

我目前正在以下列方式生成一个表:

A0 A1 A2 A3
B0 B1 B2 B3
C0 C1 C2 C3
D0 D1 D2 D3

我想做:

A0 B0 C0 D0
A1 B1 C1 D1
A2 B2 C2 D2
A3 B3 C3 D3

所以基本上让它以 4 列垂直前进。

当前代码:

<table cellspacing="2px">
    <?php

      # display products in 4 column table #

        $i=0;
        while ($data=mysqli_fetch_array($result)){
            if($i > 0 and $i % 4 == 0) {
                echo '<tr></tr>';
            }

        //passing id in link to retrieve product details
        echo '<td><a href="displayProduct.php?product_id='.$data['product_id'].'" class="myButton">'.$data['name'].'</a></td>';
        $i++;   
        }

    ?>
</table>

有人能指出我正确的方向来完成这个吗?

澄清

在上面的插图中,我只是指出我希望结果垂直向下,并且还需要扩展为 4 列(我只显示了 2,但它需要扩展为 4)。

在代码片段中,它水平进行。

我还更新了问题以保持简单。

我希望这能消除任何歧义。

4

3 回答 3

1

这应该这样做。

<table>
<?php
$i = 0;
while ($data = mysql_fetch_array($result);
    if ($i % 4 == 0) {
        if ($i > 0) {
            // End last row unless this is the first row
            echo '</tr>';
        }
        // Start a new row
        echo '<tr>';
    }
    echo '<td>...</td>';
    $i++;
}
if ($i > 0) {
    // Close out the last row, unless there wasn't any data
    echo '</tr>';
}
?>
</table>

你有<tr></tr>倒退 - 如果你想结束一排并开始新的一排,它必须是</tr><tr>。但是第一行需要特殊处理,因为没有前一行可以结束。如果您知道总会有数据,您可以无条件地输出第一个<tr>和最后一个</tr>作为 HTML 代码的一部分,但我编写代码是防御性的。

于 2013-09-12T23:23:14.537 回答
0

嗯……谁再用 TABLES 了?用 CSS 来做!很容易做到。根据需要调整我的样本。

<?
$x = 0;
$y = 0;
$count = 0;
$total = 100; // whatever your total is from sql result;
while ($count < $total) {
echo '<img src="foldername/'.$itempic[$count].'" style=\'postion:absolute;top:'.$y.'px;left:'.$x.'px;\'>'."\n";
$y = $y + 50;
if ($y > 300){
$y = 0;
$x = $x + 50;
}
$count++;
}
?>
于 2013-09-12T23:50:34.010 回答
0

这是我专门为您制作的另一个示例:是的,它有点笨拙,但是您可以从中收集逻辑并根据需要进行调整。虽然它确实有效,但我只是不打算花时间让一个例子完美。

<?
$info = Array(
A0,A1,A2,A3,
B0,B1,B2,B3,
C0,C1,C2,C3,
D0,D1,D2,D3);


$count = 0;
$total = 20;
$row = 0;
$col = 0;
while ($count < $total){
    echo $info[$col+$row];
$col++;
$row = $row +3;
    if($col > 4){
$col = 0;
$row = $row - 14;
echo '<br/>';}
$count++;
}
?>
于 2013-09-13T01:20:36.310 回答