【问题标题】:Displaying tables results in multiple columns, column by column显示表格的结果是多列,逐列
【发布时间】:2019-12-13 22:48:36
【问题描述】:

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

【问题讨论】:

    标签: php html mysql html-table


    【解决方案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>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 2013-09-01
      相关资源
      最近更新 更多