organize images from directory in table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

organize images from directory in table

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Check out the Useful Threads, we've talked about this a couple times
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post 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);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Your for loop will stop after 2 iterations.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Did you fix what I mentioned?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post by kristie380 »

I'm not sure I know how to fix what you mentioned. Can you explain?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
kristie380
Forum Commoner
Posts: 36
Joined: Sun Oct 09, 2005 10:51 pm

Post by kristie380 »

i really want them displayed in a table but i still cannot get this darn thing to work
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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).
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

kristie380 wrote:i really want them displayed in a table
Why? My recomendation is fluid, dynamic and not dependant on screen resolution.
Post Reply