[php] iterating through a folder 4 at a time

Soldato
Joined
1 Feb 2006
Posts
8,188
hi guys,

my head isn''t with it today and i'm working on a script which iterates through all the images in a folder and outputs the images in a table. I want to limit each iteration to 4 images i.e. I want to output the following..

<tr>
.... <td><img 1
.... <td><img 2
.... <td><img 3
.... <td><img 4
</tr>
<tr>
.... <td><img 5
.... <td><img 6
.... <td><img 7
.... <td><img 8
</tr>

How can I possibly iterate through the first 4 files, output a row end tag </tr> and a new row tag <tr> and then iterate through the next 4 etc.

The result is needed so that I can display the images (4 to a row in a gallery).

Sorry if this seems like a stupid request but I can't work it out!

edit:
This is the top of my code
Code:
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {


Also if I could page the output based on the total number of images that would be very handy.
 
Last edited:
Thanks for that guys... so to add that to my own code it would be something like....

Code:
if (is_dir($dir)) {
 if ($dh = opendir($dir)) {
  while (($file = readdir($dh)) !== false) {

foreach (range(0,count($file)) as $i) {
  if ($i % 4 == 0) {
    echo '</tr>......something..........<tr>';
  }
  else {
      echo '........something.......';
  }

Would this work or will it not like me embedding the foreach within a while loop?
 
Back
Top Bottom