我必须制作一个表,其中可以通过变量($cols)设置列的数量,每个单元格包含通过GLOB Array获得的图片。我现在的代码将输出与单元格和列的正确数量的表,但我需要帮助让每个图片显示出来。
<?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";
?>
您不是在索引数组以获取单个项。:
$output .= "<td><img src=$array></td>n";
应该$output .= "<td><img src="$array[$i]"></td>n";
还请注意,我正在转义双引号,以便您的HTML src属性值是双引号。
此外,如果将count($array)缓存到另一个变量中,可以使for语句更有效,尽管这可能不是什么大问题。