如何从GLOB阵列中生成此可变大小的表输出图像?

问题描述 投票:0回答:1

我必须创建一个包含大量列的表,这些列可以由变量($ cols)设置,每个单元格包含通过GLOB数组获得的图片。我现在的代码将输出一个包含正确数量的单元格和列的表格,但我需要帮助让每张图片显示出来。

<?php
$cols = 4;

$array = glob("include/*.{jpg}", GLOB_BRACE);


$output = "<table>\n";

$cell_count = 1;

for ($i = 0; $i < count($array); $i++) {
    if ($cell_count == 1) {
        $output .= "<tr>\n";
    }
    $output .= "<td><img src=$array></td>\n";
    $cell_count++;

    if ($cell_count > $cols || $i == (count($array) - 1)) {
        $output .= "</tr>\n";
        $cell_count = 1;
    }
}
$output .= "</table>\n";
echo "$output";

?>
php html html-table glob
1个回答
0
投票

您没有索引到数组以获取单个项目。这个:

$output .= "<td><img src=$array></td>\n";

应该

$output .= "<td><img src=\"$array[$i]\"></td>\n";

另请注意,我正在转义双引号,以便您的HTML src属性值被双引号。

此外,如果将count($ array)缓存在另一个变量中,则可以使for语句更有效,尽管这可能不是什么大问题。

© www.soinside.com 2019 - 2024. All rights reserved.