user8223567
user8223567

Reputation:

Display images from folder in table using php,html?

I want to display my images in table form. Here is my code:

<?php
$files = glob("images/*.*");
for ($i = 0; $i < count($files); $i++) {
    $image = $files[$i];
    $supported_file = array(
        'gif',
        'jpg',
        'jpeg',
        'png'
    );

    $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
    if (in_array($ext, $supported_file)) {
        echo basename($image);
echo '<img src="' . $image . '" alt="Random image" ,width=100px, height=100px /><br>';
} else {
continue;
}
}
?>

enter image description here

Something like that picture.

Upvotes: 2

Views: 8413

Answers (2)

Jignesh Gohil
Jignesh Gohil

Reputation: 15

Change your html code from:

echo '<img src="' . $image . '" alt="Random image" ,width=100px, height=100px /><br>';

to

  echo "<table border='1'>";
  echo "<tr><td>";
  echo basename($image);
  echo "</td><td>";
  echo '<img src="' . $image . '" alt="Random image" ,width=100px, height=100px />';
  echo "</td></tr>";
  echo "</table>";

Upvotes: 2

B. Desai
B. Desai

Reputation: 16436

If you want it in table the simply add table tag with and to it

<table>
<tr>
  <th>Image Name</th>
  <th>Image</th>
</tr>
<?php
$files = glob("images/*.*");
for ($i = 0; $i < count($files); $i++) {
    $image = $files[$i];
    $supported_file = array(
        'gif',
        'jpg',
        'jpeg',
        'png'
    );

    $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
    if (in_array($ext, $supported_file)) {
      echo "<tr><td>";
      echo basename($image);
      echo "</td><td>";
      echo '<img src="' . $image . '" alt="Random image" ,width=100px, height=100px /><br>';
      echo "</td></tr>";
} else {
continue;
}
}
?>
</table>

Upvotes: 0

Related Questions