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";
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.