Gamerz Posted August 12, 2009 Share Posted August 12, 2009 Hello, How do I make it so that it displays 3 images or X images per row...so that every 3 images, it will display a new row to start. The following codes displays all the images in a folder ($path). I want it to make it so instead of each time, a <br><br>for every image, it would make it so that every 3 images, make a new row to start it. And another thing..is there a possible to void all NONE image files, so that if something.txt gets uploaded onto uploads folder, it won't show something.txt...rather only images.. <?php $path = "uploads"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); // Loop through the files while ($file = readdir($dir_handle)) { if($file == "." || $file == ".." || $file == "filehere.php" || $file == "uploaded.php" || $file == "index.php" ) continue; echo "<center><img src =\"$file\" width=\"96\" height=\"96\"><br /><br>"; } // Close closedir($dir_handle); ?> Quote Link to comment Share on other sites More sharing options...
play_ Posted August 12, 2009 Share Posted August 12, 2009 Here: http://www.phpfreaks.com/forums/index.php/topic,264113.msg1245656.html#msg1245656 Same concept. use an $i and $i++ inside the loop to increment it Quote Link to comment Share on other sites More sharing options...
Gamerz Posted August 12, 2009 Author Share Posted August 12, 2009 Wait, so how would I make the $i and $i++ inside the following code: while ($file = readdir($dir_handle)) { Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 12, 2009 Share Posted August 12, 2009 Wait, so how would I make the $i and $i++ inside the following code: while ($file = readdir($dir_handle)) { you set $i to 0 before the while() loop is running, then increment $i on each iteration of the while() loop. Quote Link to comment Share on other sites More sharing options...
Gamerz Posted August 12, 2009 Author Share Posted August 12, 2009 Ok, my code's now: <?php $path = "uploads"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); // Loop through the files while ($file = readdir($dir_handle)) { echo '<table border="1"> <tr>'; for( $i = 0; $i < 3; $i++ ) { if( ($i % 3) == 0 ) { echo '</tr><tr>'; } if($file == "." || $file == ".." || $file == "jhey.js" || $file == "uploaded.php" || $file == "index.php" ) continue; echo "<td><center><img src =\"$file\" width=\"96\" height=\"96\"><br /><br></td>"; } echo '</table>'; } // Close closedir($dir_handle); ?> </center> When I execute that code, it displays the same images in one row, then another image in the second row...and so on.. EDITED BY akitchin: screenshot was large and didn't add any useful details. What am i doing wrong? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 12, 2009 Share Posted August 12, 2009 you should really look at the PHP manual under while() loops and for() loops to understand what they do: while() for() you're running a for() loop every single time the while() loop runs. this will force it to run three times for EACH item, and that's why you're getting 3 of the same image per row. you can drop the for() loop altogether, and just increment $i at the end of the while() loop: $i = 0; // Loop through the files while ($file = readdir($dir_handle)) { echo '<table border="1"> <tr>'; if( $i % 3 == 0 ) { echo '</tr><tr>'; } if($file == "." || $file == ".." || $file == "jhey.js" || $file == "uploaded.php" || $file == "index.php" ) continue; echo "<td><center><img src =\"$file\" width=\"96\" height=\"96\"><br /><br></td>"; $i++; } echo '</table>'; Quote Link to comment Share on other sites More sharing options...
Gamerz Posted August 12, 2009 Author Share Posted August 12, 2009 Ok, when I use that code you gave me, it only displays one image per row...but different images this time....how do i fix it? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 12, 2009 Share Posted August 12, 2009 Ok, when I use that code you gave me, it only displays one image per row...but different images this time....how do i fix it? i didn't realize you had the opening table tag being echoed within the while() loop as well. move it out of the while() loop. i'm not going to give you the code because frankly, i think you need to learn how while() loops work for yourself. Quote Link to comment Share on other sites More sharing options...
Gamerz Posted August 12, 2009 Author Share Posted August 12, 2009 Ok...I got it to work.. But one more thing: 1. How do I VOID all none image files on that display image code? So if someone uploads something.txt, it won;t display it in the code? I tried doing this on the below code: if($file == "*.txt" but it didn't work... Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 12, 2009 Share Posted August 12, 2009 Ok...I got it to work.. But one more thing: 1. How do I VOID all none image files on that display image code? So if someone uploads something.txt, it won;t display it in the code? I tried doing this on the below code: if($file == "*.txt" but it didn't work... you can check the extension of a file using strrchr on the filename. Quote Link to comment Share on other sites More sharing options...
Gamerz Posted August 12, 2009 Author Share Posted August 12, 2009 Yea, but how do I only show img files? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 12, 2009 Share Posted August 12, 2009 Yea, but how do I only show img files? ... by only choosing to display an image for files with an image extension. Quote Link to comment Share on other sites More sharing options...
Gamerz Posted August 12, 2009 Author Share Posted August 12, 2009 Ohh wait how would I code that? I coudn't find anything good on strrchr Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 12, 2009 Share Posted August 12, 2009 Ohh wait how would I code that? I coudn't find anything good on strrchr are you telling me the manual entry for strrchr, which explains what parameters it takes, what it does, and what it returns, along with user comments and example code, didn't have "anything good?" Quote Link to comment Share on other sites More sharing options...
Gamerz Posted August 12, 2009 Author Share Posted August 12, 2009 ohh nvm i got it Quote Link to comment 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.