gpt4 book ai didi

php - 将数据拆分为 3 列

转载 作者:行者123 更新时间:2023-11-29 03:58:19 25 4
gpt4 key购买 nike

我有一个包含人名的 MySQL 表。使用 PHP,将这些名称分开并显示在 3 列中的最佳方式是什么。

这可以使用 HTML 表格来实现,但是否可以仅使用 <div> 来实现?的?

我听说这可以使用 % 运算符来完成。这是最好的方法吗?如何实现?

最佳答案

您可以使用取模运算符来完成此操作,但实际上只使用 CSS 就可以做到。

使用 display: inline-block ,可以获得很好的柱子效果。看看this JSFiddle here .我只是因为懒惰才使用 JavaScript; <div>在您的情况下,列表将由 PHP 生成。如果你想把它们限制在一定的宽度,把它们放在一个容器里就可以了<div>具有固定宽度。


我想出了一个使用表格的解决方案,这确实是您应该做的(您没有给出任何特殊用例)。代码在下面,还有一个工作演示 here .

$columns = 4;       // The number of columns you want.

echo "<table>"; // Open the table

// Main printing loop. change `30` to however many pieces of data you have
for($i = 0; $i < 30; $i++)
{
// If we've reached the end of a row, close it and start another
if(!($i % $columns))
{
if($i > 0)
{
echo "</tr>"; // Close the row above this if it's not the first row
}

echo "<tr>"; // Start a new row
}

echo "<td>Cell</td>"; // Add a cell and your content
}

// Close the last row, and the table
echo "</tr>
</table>";

最后,我们有以列为中心的布局,这次回到 div秒。这里有一些 CSS;这应该放在一个单独的文件中,不要内联

<?php
$rows = 10; // The number of columns you want.
$numItems = 30; // Number of rows in each column

// Open the first div. PLEASE put the CSS in a .css file; inline used for brevity
echo "<div style=\"width: 150px; display: inline-block\">";

// Main printing loop.
for($i = 0; $i < $numItems; $i++)
{
// If we've reached our last row, move over to a new div
if(!($i % $rows) && $i > 0)
{
echo "</div><div style=\"width: 150px; display: inline-block\">";
}

echo "<div>Cell $i</div>"; // Add a cell and your content
}

// Close the last div
echo "</div>";
?>

关于php - 将数据拆分为 3 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7138954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com