gpt4 book ai didi

php - 使用 PHP 在表格中从上到下而不是从左到右打印数组

转载 作者:可可西里 更新时间:2023-10-31 22:09:13 26 4
gpt4 key购买 nike

我正在尝试编写一个逻辑来在表中从上到下而不是从左到右显示记录。

这是我的尝试:

<?php $options = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); ?>
<table border="1" colspan="0" cellspan="0">
<?php
$htmldata = "";
$counter = 1;

$rowcount = ceil(count($options) / 3);

for ($i = 0; $i < count($options); $i++) {
echo $counter % 3 ."<br/>";

if ($counter % 3 == 1) {
$htmldata .= "<tr>";
$htmldata .= "<td>".$options[$i]."</td>";
}
if ($counter % 3 == 2) {
$htmldata .= "<td>".$options[$i]."</td>";
}
if ($counter % 3 == 0) {
$htmldata .= "<td>".$options[$i]."</td>";
$htmldata .= "</tr>";
}
$counter++;
}
echo $htmldata;
exit;
?>
</table>

下面是表格中的当前代码输出。

Actual output

但我需要表格这样输出。

Expected output

我怎样才能做到这一点?
你们能帮我写这个逻辑吗?
另外,它目前显示的是 10 条记录,但可能会更多或更少,因此我们需要代码是动态的。

任何帮助将不胜感激。提前致谢!

注意:对于任意数量的记录,它必须只有 3 列,并且对于 10 条记录,输出必须与第二个图片示例中显示的相同。

最佳答案

希望是我的最终答案(经过所有编辑)

<?php
// feel free to change the values in these variables
$options = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
$numCols = 3; // the number of columns to present
$pad = false; // whether to add blank <td> in final row

// don't alter these variables
$rows = (int)ceil(count($options) / $numCols); // number of rows of the column with most rows
$c = count($options) % $numCols; // how many columns have records on the final row
$col = 1; // current column

// iterate over the columns
for ($j = 0; $j < $numCols; $j++) {
// iterate over rows
for ($i = 0; $i < $rows; $i++) {
// if iteration is last line and on a column with extra data
// or if the iteration is not the last row
// or if the last row perfectly fits the column count
if (($col <= $c && $i == $rows - 1) || $i != $rows - 1 || $c == 0) {
// remove the first element from the options array
$arr[$i][$j] = array_shift($options);
}
}
// move onto the next column
$col++;
}

// create the extra <td> elements in the final row
if ($pad) {
$arr[count($arr) - 1] = array_pad($arr[count($arr) - 1], $numCols, null);
}
?>
<table border="1" colspan="0" cellspan="0">
<?php for ($i = 0; $i < count($arr); $i++) { ?>
<tr>
<?php for ($j = 0; $j < count($arr[$i]); $j++) { ?>
<td><?= $arr[$i][$j]; ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>

我已经编辑了我最初发布的答案,因为它已损坏且冗长。这个解决方案比以前的解决方案更简洁,效果更好。

在撰写此答案时,我还没有看到有关固定列数的编辑,因此有一个变量可以在必要时更改列数。随意更改列数 ( $numCols ) 以及数组中的元素数。如果您需要更改应用程序的要求,可以在这里查看。

如果您愿意,可以摆弄一些东西。它们如下:

  • $options
    这只是您的数据数组。这可以是任何你想要的。表格将根据要求从上到下根据这些数据创建。
  • $numCols
    也许你想重命名这个,这真的没关系。它是“列数”的缩写,就是这样;无论你在这里输入什么整数,你都会得到那么多 <td>该行中的元素,或者,如果您不想以 HTML 方式阅读它们,那么您将拥有那么多列以及后续行计数,具体取决于提供的数据。
  • $pad
    这是因为我注意到最后一行的记录较少 - 除非 $options长度可以被 $numCols 整除,没有余数.
    为了解决这个问题,我添加了这个选择。它在 for 循环创建数组后使用,并且只会用空值填充最后一行,这样您就不会在最后一行的右侧有空格。
    当然,这完全取决于您是否想要。就个人而言,我会选择设置 $pad为真,因为我认为它使表格看起来更整洁。

我建议您不要更改其余代码,因为它会正常工作。
显然,如果你经常使用这段代码,那么你可以将它包装在一个函数中; 尽管如果您这样做,我强烈建议您不要像最初那样做并开始创建一个字符串来存储您的表
这是不好的做法,可能会导致错误。例如,您可能忘记关闭表格 - 您通过使用 exit 关键字完成的操作 - 许多现代浏览器不会注意到这一点,因为它们仍会显示所需的结果,但您的代码随后将无法通过验证器。因此,如果您希望使用函数,请确保它只是 PHP 代码。然后只需在数组上调用它并像我最后所做的那样进行迭代(我的意思是,带有 for 循环的表部分不是函数的一部分)。

老实说,我不敢相信我花了多长时间做这件事!

Output of code

关于php - 使用 PHP 在表格中从上到下而不是从左到右打印数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46543610/

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