Jump to content

Creating a table filled with images created from file names in a directory


yellowepi

Recommended Posts

I'm attempting to create code that will take the file names from a folder, insert the names into an image tag, and insert the images into a table.

 

The problem with my code is that it uses the same image in every row.  What would an easy way to be to fix this?

 

<?php
function printtd() {
$dirname = "imagegallery";
$dh = opendir($dirname) or die("couldn't open directory");
while ($file = readdir($dh)) {	
if(preg_match("(jpg|bmp|jpeg|png|gif)", $file)) {

	echo "<img src='imagegallery/".$file."' height='15 %'/>";
}
}

closedir($dh); 
}
?>


<?php
echo "<table> \n";
for ($y=1;$y<=2;$y++) {
echo "<tr> \n";
for ($x=1;$x<=6;$x++) {
echo "<td> \n";
printtd();
echo"</td> \n";
}
echo"</tr> \n";
}
echo"</table> \n";
?>

Your logic is wrong. You have to get the list of files first and then use them in the for loops. Also, I would use the glob function to get the files:

<?php
$images = glob('imagegallery/*.{jpg,bmp,jpeg,png,gif}',GLOB_BRACE);
$i = 0;
echo "<table> \n";
for ($y=1;$y<=2;$y++) {
   echo "<tr> \n";
   for ($x=1;$x<=6;$x++) {
       echo '<td><img src="' . $images[$i++] . '">' . "</td>\n";
   }
   echo"</tr> \n";
}
echo"</table> \n";
?>

 

Ken

Hi there,

 

Hope you don't mind but I've been trying to do this too without success. I tried kenrbnsn's version which is closer but I get the first image of each row repeated. Which seems to be what yellowepi was saying was happening originally.

here's my code:

 echo "<table>";
echo "<tr ><td colspan = '5'>";
echo "<h1>Category: " . $category_name . " </h1>\n";
echo "</td></tr>";
$ImageDir = "img/category/";
$ImageThumb = $ImageDir . "/thumbnails/";
  
  $getpic = "SELECT image_id, image_group, image_name, location_no " .
//$getpic = "SELECT image_id " .
               "FROM cms_images_category " .
		   "WHERE category_name = '" . $_GET['category'] . "'";
               "ORDER BY image_group ";
		   
      $results = mysql_query($getpic,$conn)
        or die(mysql_error());

    while ($row = mysql_fetch_array($results)) {
  extract($row);
$images = $ImageThumb . $image_id;

$i = 0;
//echo "<table> \n";
for ($y=1;$y<=1;$y++) {
   echo "<tr> \n";
   for ($x=1;$x<=3;$x++) {
	echo "<td><h2>" . $image_group . "</h2></td>\n";
	echo "<td><a href =\"".$ImageDir . $image_id . ".jpg\" target='top'>"; 
    echo "<img src=\"".$images . ".jpg\"></a></td>\n";
	echo "<td><h2>" . $image_name . "</h2></td>\n";
   }
   echo"</tr> \n";
}	 
} 	
echo "<tr><td><p>Click <a href ='search.php'>here</a> to return to categories<p></td></tr></table>";

 

probably not very tidy code either. If someone could point me in the right direction that would be awesome. Also the amount of images in a row seems to be determined by the $x<=3, is there a way of the screen width determining this?

 

Thanks in advance.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.