0

我必须制作一个表格,其中包含可以由变量($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";

?>
4

1 回答 1

0

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

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

应该

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

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

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

于 2013-05-07T19:22:20.190 回答