簡體   English   中英

在多列中逐列顯示表格結果

[英]Displaying tables results in multiple columns, column by column

我在一個有 5 列的表格中顯示了我的結果。 整個頁面讀取結果然后<TR>開始新行並顯示下一行結果(請參閱http://www.dsoba.co.uk/immemoriam/index2.php )我可以先顯示它們以進行填充嗎一列然后下一列? 掃描列比掃描行更容易。

            $result = mysqli_query($con,"SELECT * FROM memberlist where deceased='d' order by surname, initials");
            $tmp = 0; # initiate the tmp variable
    echo "<table class='table1'>
            <tr>";
        while($row = mysqli_fetch_array($result))
             {  # start of the while loop, once for each record

                # note removed the <BR> from the main output of the loop:
    echo "<td>" . $row['initials'] . " " . $row['surname'] . " " . $row['yearstart'] . " - " . $row['yearleft'] . "</td>";
        $tmp = ++$tmp; # increment 'tmp' by 1
        if ($tmp == 5)  {
        $tmp = 0;
    echo "</tr><tr>";  # new line if you've echoed 5 records
                        }

             }    # this is the end of the while loop


    echo "</tr></table>";

您可以將數據保存到每列的數組中。

$columns = [];
$i = 0;
while($row = mysqli_fetch_array($result)) {
    $columns[$i++ % INT_COLUMNS][] = $row;
}

foreach ($i = 0; $i < count($columns[0]); $i++) {
    echo '<tr>';
    foreach ($j = 0; $j < INT_COLUMNS; $j++) {
        if (isset($columns[$j][$i]) {
            echo '<td>...</td>';
        }
    }
    echo '</tr>';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM