【问题标题】:PHP Dynamic Table Data Results Vertically not HorizontallyPHP动态表数据结果垂直而不是水平
【发布时间】:2017-12-31 21:49:18
【问题描述】:

我有一个数据数组,希望在动态表中显示,但需要垂直显示。

目前我得到以下输出:

示例 1:

1 - 2 - 3 - 4
5 - 6 - 7 - 8
9 - ... etc.

但我需要这样:

示例 2:

1 - 4 - 7
2 - 5 - 8
3 - 6 - 9

我在 Google 和堆栈上进行了搜索,但找不到可以用来解决我的问题的直接答案。

$array = array("4|Four","12|Twelve","2|Two","5|Five","11|Eleven","3|Three","1|One","6|Six","10|Ten","8|Eight","7|Seven","9|Nine");

$maxcols = 3;
$i = 0;

$table = "<table width=\"80%\" border=\"0\">\n";
$table .= "<tr>\n";


if(!empty($array))
{
rsort($array,SORT_NUMERIC);
for($p=0;$p<sizeof($array);$p++)
{
list($num,$title) = explode("|", trim($array[$p]));


if ($i == $maxcols) 
{
$i = 0;
$table .= "</tr>\n<tr>\n";
}
$table .= "<td>"."[ ".$num." ]"." ".$title."</td>\n";
$i++;
}

while ($i < $maxcols) 
{
$table .= "<td>&nbsp;</td>\n";
$i++;
}

$table .= "</tr>\n";
$table .= "</table>\n";
}

echo $table;

上面的代码输出如第一个示例所示,但我无法将其输出,如第二个示例所示。

【问题讨论】:

  • 数组索引总是整数?
  • @frz3993 如示例代码中所示.. 我需要数字垂直运行,如示例 2 所示
  • @frz3993 我已经编辑了帖子以显示示例数组:)
  • 那么,如果您有 10 个值而不是示例中的 9 个呢?它会开始一个新的专栏吗?
  • @Don'tPanic 嗨.. 理想情况下我希望它开始一个新行.. 我想通过变量选择多少列:) 谢谢

标签: php dynamic html-table


【解决方案1】:

我原本想用 array_chunk 将数组分成几行,但后来我突然想到只用数学也可以做到。

$rows = 3; // define how many rows as you want

$array = array("4|Four","12|Twelve","2|Two","5|Five","11|Eleven","3|Three","1|One",
    "6|Six","10|Ten","8|Eight","7|Seven","9|Nine");
rsort($array,SORT_NUMERIC);

// determine number of columns needed to fit the values into the number of rows you want
$columns = count($array) / $rows; 

$table = '<table width="80%" border="0">';
for ($i=0; $i < $rows; $i++) { 
    $table .= '<tr>';
    for ($j=0; $j < $columns; $j++) {
        $index = (int) ($j * $rows + $i); // Do the math to get the proper array index
        if (isset($array[$index])) {                    
            list($num,$title) = explode("|", trim($array[$index]));
            $table .= "<td>"."[ ".$num." ]"." ".$title."</td>\n";
        } else {
            $table .= '<td>&nbsp;</td>';
        }
    }
    $table .= '</tr>';
}
$table .= '</table>';

上面的例子产生了一个像这样的网格,其中数字是数组键。

0  3  6  9
1  4  7  10
2  5  8  11

产生这个带有两个 for 循环的网格的数学是这样的:

0*3+0  1*3+0  2*3+0  3*3+0
0*3+1  1*3+1  2*3+1  3*3+1
0*3+2  1*3+2  2*3+2  3*3+2

或用文字表示,(列索引 * 总行数)+ 行索引。无论您要指定行数还是列数,嵌套循环部分都将保持不变。唯一需要更改的是一开始的数学计算,以计算指定列数需要多少行,反之亦然。如果您将开始部分更改为

$columns = 3; 
$rows = ceil(count($array) / $columns);

那么您应该能够指定列数而不是行数。 ceil 是必需的,因为除法的任何剩余部分都必须四舍五入才能获得所需的总行数。我认为这就是您在第一次切换行和列时得到重复值的原因。

【讨论】:

  • 非常感谢您提供如此详细的答案,非常感谢代码中的 cmets 太棒了。为了我的需要,我切换了行和列,因此我可以将列分配为变量,并让代码计算出行:) 再次欢呼!
  • 我认为我做对了,但仍然不是 100%.. 你的行示例有效,但我想对列执行此操作.. 我尝试交换代码,但我得到了重复的每列的最后一个条目在下一列的开头.. 任何想法我可以如何将行 = 3 更改为列 = 3.. 很抱歉很痛苦非常感谢
  • 别担心,等我回到电脑前再看一遍。
  • 再次感谢您的帮助和详细的解释......这很完美!
【解决方案2】:

我就是这样做的。

    <table align="center" style="width:40%;">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<?
$rotate = 0;
$amount = 1;
while($amount <= 50)
{
if($rotate == 0) {
echo '<tr>
<td>test</td>';
$rotate = 1;
}
else if($rotate == 1) {
echo '<td>test2</td>';
$rotate = 2;
}
else if($rotate == 2) {
echo '<td>test3</td>';
$rotate = 0;
echo '</tr>';
}

$amount++;

}
echo '</tr></table>';

这将在一个空白的 php 页面上工作 - 所以你可以看到它是如何工作的。

【讨论】:

  • 您好,感谢您的帮助。我尝试用我的代码实现你的代码,但我得到了相同的输出。这是截图link这是我已经拥有的但我需要示例2
  • @John 也许我不明白你到底想要什么。
【解决方案3】:

为了进一步扩展@DontPanic 的答案,可以将转换数组键的问题抽象为您可以拥有任意数量的列。两种实现数学基本上是等价的。

// returns a closure that can be used to translate the index order

function getIndexTranslator($list, $numColumns) {
    $numRows = ceil(count($list)/$numColumns);
    return function($x) use ($list, $numColumns, $numRows){
        return ($x % $numColumns) * $numRows + floor($x / $numColumns);
    };
}

然后您可以使用返回的函数让您按照所需的文档顺序遍历元素。以下是您将如何使用上述函数。

$transformer = getIndexTranslator($arr, 3);

for($x = 0; $x < count($arr); $x++){
    echo $arr[$transformer($x)], $x % 3 == 2?"\n":" ";
}

玩转你自己here

【讨论】:

  • 感谢您抽出宝贵时间发布解决方案 - 我也会玩这个:)
猜你喜欢
  • 2019-02-27
  • 2022-01-22
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 2017-05-27
  • 1970-01-01
  • 2015-01-18
  • 2016-11-21
相关资源
最近更新 更多