craigtb Posted September 1, 2007 Share Posted September 1, 2007 Hello, I have a picture gallery that will automatically add pictures if they have the right name and all i have to do is add rows to my loop. Right now i have it so there are four thumbnails per row. The problem is that when the number is not divisible by 4 evenly it will still add pictures in a number that is evenly divisible by four but they will just be dead images. How would i go about making if there is not a number of pictures that is even divisible by four to make it not add the extra dead images. here is the code. <?php $num = 1; print "<table border=\"0\">\n"; for ($y = 1; $y <= 15; $y++) { print "<tr>\n"; for ($x=1; $x<=4; $x++) { print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; ++$num; } print "</tr>\n"; } print "</table>"; ?> I know what i have said might be a little confusing, and i am sorry for that. thanks in advance, Craig Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/ Share on other sites More sharing options...
Fadion Posted September 1, 2007 Share Posted September 1, 2007 Im not quite getting it but im trying to guess. With divisible by 4 u mean having lets say 16 images so it shows them in 4 rows with 4 images in a row? So if u have 15 u want 3 rows with 4 images and one with 3 images? If so which varialbe should be divisible by 4? Anyway u can try this: if(file_exists('pages/gallery/$num.jpg')){ //print the image } Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-339039 Share on other sites More sharing options...
craigtb Posted September 1, 2007 Author Share Posted September 1, 2007 Yea i think you understand it, but when i tried using that it wont display anything now. It is very likely i placed it in the wrong place. Any advice? <?php $num = 1; print "<table border=\"0\">\n"; for ($y = 1; $y <= 15; $y++) { print "<tr>\n"; for ($x=1; $x<=4; $x++) { if(file_exists('pages/gallery/$num.jpg')){ //print the image print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; } ++$num; } print "</tr>\n"; } print "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-339043 Share on other sites More sharing options...
Fadion Posted September 1, 2007 Share Posted September 1, 2007 U placed it right. I assume u have images 1.jpg, 2.jpg etc, so dont know why its not working. Also where is </td>, i cant see it? Try using file_exists() with a file u know that exists (ex. file_exists('myfile.jpg')) and see if u get true or false. Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-339058 Share on other sites More sharing options...
craigtb Posted September 1, 2007 Author Share Posted September 1, 2007 Ok i tested it and the file_exists function does work with a random file. And i am not sure about the <td></td>, the other file seems to work without it. Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-339064 Share on other sites More sharing options...
Jessica Posted September 1, 2007 Share Posted September 1, 2007 Loop through the list of files and use a technique like this for placing your table rows: http://www.phpfreaks.com/forums/index.php/topic,157422.msg685107.html#msg685107 Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-339074 Share on other sites More sharing options...
craigtb Posted September 3, 2007 Author Share Posted September 3, 2007 Alright i looked at that and it seems pretty similar to something i've done before. This is what i have now. while ($newarray = 55) { if ($count % NUMCOLS == 0) echo "<TR>\n"; # new row echo "<TD>"; print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; echo "</TD>\n"; $count++; if ($count % NUMCOLS == 0) echo "</TR>\n"; # end row } if ($count % NUMCOLS != 0) { # end row if not already ended while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</TR>\n"; } echo "</TABLE>"; But whenever i run it, it freezes up firefox, which makes me belive that it actually has no limits and jsut continues through the loop forever. Also what does show up is that its just one column. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-340603 Share on other sites More sharing options...
craigtb Posted September 3, 2007 Author Share Posted September 3, 2007 whoops i meant to have define ("NUMCOLS",2); $count = 0; echo "<TABLE border=1>"; while ($newarray = 55) { if ($count % NUMCOLS == 0) echo "<TR>\n"; # new row echo "<TD>"; print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; echo "</TD>\n"; $count++; if ($count % NUMCOLS == 0) echo "</TR>\n"; # end row } if ($count % NUMCOLS != 0) { # end row if not already ended while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</TR>\n"; } echo "</TABLE>"; Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-340623 Share on other sites More sharing options...
AndyB Posted September 3, 2007 Share Posted September 3, 2007 while ($newarray = 55) ... I assume you meant == (the equality operator, not the assignment operator =). Either way, this is a never-ending condition based on the code snippet you posted. Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-340628 Share on other sites More sharing options...
craigtb Posted September 3, 2007 Author Share Posted September 3, 2007 Yes i did mean ==. But that is all the PHP i have in the file, the rest is just general html Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-340665 Share on other sites More sharing options...
AndyB Posted September 3, 2007 Share Posted September 3, 2007 Then since the value of $newarray is never changed in the loop, the loop will run forever. Time to re-think your logic. Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-340723 Share on other sites More sharing options...
craigtb Posted September 3, 2007 Author Share Posted September 3, 2007 should this work a little better? define ("NUMCOLS",4); $count = 0; echo "<TABLE border=1>"; while ($newarray >= 55) { if ($count % NUMCOLS == 0) echo "<TR>\n"; # new row echo "<TD>"; print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; echo "</TD>\n"; $count++; if ($count % NUMCOLS == 0) echo "</TR>\n"; # end row } if ($count % NUMCOLS != 0) { # end row if not already ended while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</TR>\n"; $newarray++; } echo "</TABLE>"; Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-340741 Share on other sites More sharing options...
craigtb Posted September 3, 2007 Author Share Posted September 3, 2007 Ok scratch that last post, i found lots of errors. Now it is working but instead of cycling through all the pictures it just continually shows image 1 for every place. here is the code now define ("NUMCOLS",4); $count = 0; $num = 1; echo "<TABLE border=1>"; $newarray = 1; while ($count <= 55) { if ($count % NUMCOLS == 0) echo "<TR>\n"; # new row echo "<TD>"; print "\t<td><a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; echo "</TD>\n"; $count++; if ($count % NUMCOLS == 0) echo "</TR>\n"; # end row } if ($count % NUMCOLS != 0) { # end row if not already ended while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</TR>\n"; $newarray++; ++$num; } echo "</TABLE>";[\code] Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-340751 Share on other sites More sharing options...
AndyB Posted September 5, 2007 Share Posted September 5, 2007 Now it is working but instead of cycling through all the pictures it just continually shows image 1 for every place. Because $num is set to 1 and never changes. You're using $count to increment through the loop in your code. Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-342003 Share on other sites More sharing options...
craigtb Posted September 5, 2007 Author Share Posted September 5, 2007 well that fixed that problem but it is still showing dead images. Does anyone know why? Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-342084 Share on other sites More sharing options...
craigtb Posted September 6, 2007 Author Share Posted September 6, 2007 I am totally trying to rewrite this and am getting some errors that i can not figure out. it is saying Parse error: syntax error, unexpected $end on like 37, which is the last line of the file. here is the complete file. <html> <head> <title></title> </head> <body> <font color="FFFFFF"> <center> <?php $numcols = 4; $count = 0; $num = 1; echo "<table><tr>"; while ($count <= 55) { if ($count % $numcols == 0) { echo "</tr>"; echo "<tr>\n"; echo "<td>"; echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; echo "</td>"; $count++; $num++; } elseif ($count % $numcols != 0) { echo "<td>"; echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; echo "</td>; $count++; $num++; } } ?></tr></table></center></font> </body> </html> any help would be great, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-342549 Share on other sites More sharing options...
AndyB Posted September 6, 2007 Share Posted September 6, 2007 Try this. if ... else is adequate, you don't need if elseif: <html> <head> <title></title> </head> <body> <font color="FFFFFF"> <center> <?php $numcols = 4; $count = 0; $num = 1; echo "<table><tr>"; while ($count <= 55) { if ($count % $numcols == 0) { echo "</tr>"; echo "<tr>\n"; echo "<td>"; echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; echo "</td>"; } else { echo "<td>"; echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; echo "</td>; } $count++; $num++; } ?> </tr></table></center></font> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-342563 Share on other sites More sharing options...
craigtb Posted September 6, 2007 Author Share Posted September 6, 2007 Nah its still giving me the same error. Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-342676 Share on other sites More sharing options...
AndyB Posted September 6, 2007 Share Posted September 6, 2007 Change: echo "</td>; } $count++; $num++; } to: echo "</td>"; // added closing quote } $count++; $num++; } With that change, the code runs without error. Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-342850 Share on other sites More sharing options...
craigtb Posted September 6, 2007 Author Share Posted September 6, 2007 Ok that got it working but its still adding the extra dead image. So what i am trying is to say is that if it exists put the picture but if it doesnt put . But i think somehtign is wrong with my file_exists() because its saying none of them exist. Here is my code. <html> <head> <title></title> </head> <body> <font color="FFFFFF"> <center> <?php $numcols = 4; $count = 0; $num = 1; echo "<table><tr>"; while ($count <= 55) { if ($count % $numcols == 0) { echo "</tr>"; echo "<tr>\n"; echo "<td>"; if (file_exists('pages/gallery$num.jpg')) { echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; } else { echo " "; } echo "</td>"; $count++; $num++; } else { echo "<td>"; if (file_exists('pages/gallery$num.jpg')) { echo "<a href=\"pages/gallery/$num.jpg\" ><img border=\"0\" src=\"pages/gallery/thumb_$num.jpg\" alt=\"$num\" title=\"$num\" width=\"100\" height=\"90\"></a>"; } else { echo " "; } echo "</td>"; $count++; $num++; } } ?></tr></table></center></font> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/67519-picture-gallery/#findComment-342976 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.