Jump to content

[SOLVED] PHP or HTML - Display 3 images per row


Gamerz

Recommended Posts

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);
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>'; 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.