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
kristie380
Forum Commoner
Posts: 36 Joined: Sun Oct 09, 2005 10:51 pm
Post
by kristie380 » Wed Nov 01, 2006 12:49 pm
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?
kristie380
Forum Commoner
Posts: 36 Joined: Sun Oct 09, 2005 10:51 pm
Post
by kristie380 » Wed Nov 01, 2006 1:44 pm
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);
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Wed Nov 01, 2006 2:31 pm
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 » Wed Nov 01, 2006 2:47 pm
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?
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Wed Nov 01, 2006 2:56 pm
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 » Wed Nov 01, 2006 2:58 pm
I'm not sure I know how to fix what you mentioned. Can you explain?
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Wed Nov 01, 2006 3:06 pm
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.
bokehman
Forum Regular
Posts: 509 Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)
Post
by bokehman » Wed Nov 01, 2006 3:08 pm
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 » Thu Nov 02, 2006 1:44 am
i really want them displayed in a table but i still cannot get this darn thing to work
aaronhall
DevNet Resident
Posts: 1040 Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:
Post
by aaronhall » Thu Nov 02, 2006 2:10 am
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).
bokehman
Forum Regular
Posts: 509 Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)
Post
by bokehman » Thu Nov 02, 2006 6:48 am
kristie380 wrote: i really want them displayed in a table
Why? My recomendation is fluid, dynamic and not dependant on screen resolution.