Page 1 of 1

organize images from directory in table

Posted: Wed Nov 01, 2006 12:49 pm
by kristie380
I am trying to come up with a script that will print my images in a table with 3 pictures per row automatically. Here is what I have so far:

Code: Select all

if (is_dir("/reunion_photos/$id")) {

$dirname = "/reunion_photos/$id"; 
          $dh = opendir($dirname) or die("<i>You have not added any pictures</i>"); 
		  
		   while (false !== ($file = readdir($dh))) {
				 if ($file != "." && $file != "..") {
				 
$image = "$dirname/$file";

 $size = getimagesize("$image");
       $height = $size[1];
       $width = $size[0];
     if ($height > 150)
         {
               $height = 150;
               $percent = ($size[1] / $height);
               $width = ($size[0] / $percent);
         }
     else if ($width > 150)
         {
               $width = 150;
               $percent = ($size[0] / $width);
               $height = ($size[1] / $percent);
         }
		
    for($x=1; $x<=1; $x++)
{
        
echo "<td align=\"center\"><img src=\"$image\" height=\"$height\" width=\"$width\"></td>";

if($x % 3 == 0)
	{
		echo "</tr>
		<tr>";
	}
}
                                } 
								}
                                 closedir($dh);
Any ideas?

Posted: Wed Nov 01, 2006 1:11 pm
by John Cartwright
Check out the Useful Threads, we've talked about this a couple times

Posted: Wed Nov 01, 2006 1:44 pm
by kristie380
am still having trouble. now it is just posting 2 columns in my table and printing each picture twice.


Code: Select all

if (is_dir("/reunion_photos/$id")) {

$dirname = "/reunion_photos/$id"; 
          $dh = opendir($dirname) or die("<i>You have not added any pictures</i>"); 
		  
		   while (false !== ($file = readdir($dh))) {
				 if ($file != "." && $file != "..") {
				 
$image = "$dirname/$file";

 $size = getimagesize("$image");
       $height = $size[1];
       $width = $size[0];
     if ($height > 150)
         {
               $height = 150;
               $percent = ($size[1] / $height);
               $width = ($size[0] / $percent);
         }
     else if ($width > 150)
         {
               $width = 150;
               $percent = ($size[0] / $width);
               $height = ($size[1] / $percent);
         }
		 
		
    for($x=0; $x<=1; $x++)
{
        
echo "<td align=\"center\"><img src=\"$image\" height=\"$height\" width=\"$width\"></td>";

if($x % 3 == 0)
	{
		echo "</tr>
		<tr>";
	}
}
                                } 
								}
                                 closedir($dh);

Posted: Wed Nov 01, 2006 2:31 pm
by pickle
Your for loop will stop after 2 iterations.

Posted: Wed Nov 01, 2006 2:47 pm
by kristie380
I tried this viewtopic.php?t=25105 under Useful Threads and it's not working for me. I'm not posting any info from my database, only the files that are in my directory. Any suggestions for organizing files from a directory into the 3 column table?

Posted: Wed Nov 01, 2006 2:56 pm
by pickle
Did you fix what I mentioned?

Posted: Wed Nov 01, 2006 2:58 pm
by kristie380
I'm not sure I know how to fix what you mentioned. Can you explain?

Posted: Wed Nov 01, 2006 3:06 pm
by pickle
In the for loop, $x is initialized to 0. Your condition then states to continue the loop as long as $x is less than or equal to 0 - which means you're only going to iterate through it twice.

Posted: Wed Nov 01, 2006 3:08 pm
by bokehman
I hate tables in this context. Why don't you use a list? It does away with the need to control this with the html code.

Posted: Thu Nov 02, 2006 1:44 am
by kristie380
i really want them displayed in a table but i still cannot get this darn thing to work

Posted: Thu Nov 02, 2006 2:10 am
by aaronhall
Go through and think about what the script is doing. You are cycling through each file in the directory. For each file, you are printing it out twice with that for loop. It needs to come out. You just need some logic at the bottom of the while loop that decides when to put the closing/opening TR tags (every three times or so).

Posted: Thu Nov 02, 2006 6:48 am
by bokehman
kristie380 wrote:i really want them displayed in a table
Why? My recomendation is fluid, dynamic and not dependant on screen resolution.