[英]Displaying tables results in multiple columns, column by column
I have displayed my results in a table with 5 columns.我在一个有 5 列的表格中显示了我的结果。 The results read across the page then a
<TR>
starts a new line and the next row of results is displayed (see http://www.dsoba.co.uk/immemoriam/index2.php ) Can I display them to first fill a column then the next column?整个页面读取结果然后
<TR>
开始新行并显示下一行结果(请参阅http://www.dsoba.co.uk/immemoriam/index2.php )我可以先显示它们以进行填充吗一列然后下一列? Its easier to scan a column than a row.扫描列比扫描行更容易。
$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>";
You can save your data into arrays per column.您可以将数据保存到每列的数组中。
$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.