显示表格的结果是多列,每列

时间:2019-08-06 11:31:53

标签: php html mysql html-table

我已将结果显示在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>";

1 个答案:

答案 0 :(得分:1)

您可以将数据每列保存到数组中。

$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>';
}