I have to make a table with an amount of columns that can be set by a variable ($cols) with each cell containing a picture obtained through a GLOB Array. The code I have now will output a table with the correct amount of cells and columns but I need help getting each picture to show up.
<?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";
?>
You are not indexing into the array to get a single item. This:
$output .= "<td><img src=$array></td>\n";
should be
$output .= "<td><img src=\"$array[$i]\"></td>\n";
Note also that I am escaping the double quotes so that your HTML src attribute value is double quoted.
Also, you could make the for statement more efficient if you cache count($array) in another variable, although it might not be a big deal.