PHP动态表数据结果垂直不水平

时间:2015-07-24 20:24:43

标签: php dynamic html-table

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

目前我得到以下输出:

示例1:

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

但我需要这样:

示例2:

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

我已经搜索了谷歌和堆栈,但无法找到我可以用来解决问题的直接答案。

$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;

上面的代码输出如第一个例子中所示,但我无法理解它的输出,如第二个例子中所示。

3 个答案:

答案 0 :(得分:3)

我最初想过使用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是必需的,因为除法的任何其余部分必须向上舍入以获得所需的总行数。我认为这就是您在第一次切换行和列时获得重复值的原因。

答案 1 :(得分:0)

这就是我做到的。

    <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页面上工作 - 所以你可以看到它是如何工作的。

答案 2 :(得分:0)

为了进一步扩展@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

相关问题