tags:

views:

167

answers:

9

I'm using DirectoryIterator to list files. In this scenario my dir contains 19 files.

I need to create a list which wraps 7 files into a <div>.

I must be tired, because I'm not able to do this simple task.

My code is updated to reflect suggestions below:

$i = 0;
echo '<div class="first_div">';

foreach ($dir as $fileinfo) {
  if ($fileinfo->isFile()) {
    if(($i % 7 == 0) && ($i > 0) )
      echo '</div><div>';

    echo 'My file';
    $i++;
  }
}
echo '</div>';

Any help appreaciated.

+1  A: 

Somesthing similar to this:

$c = 0;
foreach ($files as $file) {
  echo $file;
  if ($c % 7 == 0) {
    //7th file
  }
  $c++;
}
Hippo
+1  A: 

Using mod 7

$counter += 1;
if($counter % 7 = 0)
    //New div

Not 100% sure on syntax for this i Php but ther must be some

David Mårtensson
+7  A: 

Something along these lines:

echo '<div>';

$i = 0;
foreach ($dir as $fileinfo) {
   if($i % 7 == 0 && $i != 0) {
       echo '</div><div>';
   }
   // do stuff
   $i++;
}

echo '</div>';
Tatu Ulmanen
Colin O'Dell
Or maybe it won't, since @Steven is saying $i starts at 2 and not at 0, as I expected.
Colin O'Dell
@webbiedave: In a comment on my answer below
Colin O'Dell
Thanks. This worked :)
Steven
Modulus to the rescue!
jathanism
Actually, using this gives one problem. The first `DIV` will only contain 5 items, since `$i` starts at 2. Therefore I had to go with Collins solution.
Steven
+2  A: 

Try using the mod operator (%) to determine if the current file number is the 7th one:

echo "<div>";
$i = 0;
foreach ($dir as $fileinfo) {
   // Remainder is 0, so its the first of 7 files. 
   // Skip this for the first one, or we'll get a blank div to start with
   if($i % 7 == 0 && $i>0) echo "</div><div>";
   echo $filenamehere;
   $i++;
}
echo "</div>";

(Code is untested but should work)

EDIT : Used an independent counter for $i, since the index seemed to start at 2 and not 0 as expected.

Colin O'Dell
It's not quite working. I'm using `$fileinfo->isFile()` so the $i always starts on 2 when I start adding files. Also, the div is addad for each file.
Steven
I updated the code to fix those issues you mentioned, try giving it another go and let me know if it works.
Colin O'Dell
My updated code no longer relies on the index, but uses a plain ol' counter which increments by 1 for each file. This should make the "index starts at 2" issue irrelevant.
Colin O'Dell
A: 

Something along these lines:

<div style="float: left; width: 200px;">
    <?php
        $count = 0;
        while ( $there_are_files_to_display )
        {
            echo ++$count . ") " . $file_name . "<br>\n";
            if ( $count % 7 == 0 )
            {
                ?>
                </div>
                <div style="float: left; width: 200px;">
                <?php
            }
        }
    ?>
</div>
<div style="clear: both;"></div>

Didn't test it but I have implemented such a thing in the past that went something along the above mentioned lines. The 7 can be replaced with ceil($file_count/$desired_number_of_columns).

Salman A
+3  A: 

or ssomething like

$chunk_size = 7;
foreach (array_chunk($dir, $chunk_size) as $chunk) {
   echo '<div>';
   foreach ($chunk as $fileinfo) {
      // echo list item $fileinfo here
   }
   echo '</div>';
}
Yanick Rochon
+1  A: 

You can use array_chunk() function to split an array to chunks.

<?
$dir_chunks = array_chunk($dir, 7);

foreach($dir_chunks as $dir)
{
    echo '<div>';
    foreach ($dir as $fileinfo) {
      if ($fileinfo->isFile()) {
         // build list here
      }
    }
    echo '</div>';
}   
?>
Silver Light
+1 for a way of not using % and mentioning array_chunk();
Nils Riedemann
+1  A: 

Or (throwing out the % operator and putting in more SPL)

<?php
$path = '......';
$nrit = new NoRewindIterator(new DirectoryIterator($path));
while ( $nrit->valid() ) {
  echo "<div>\n";
  foreach( new LimitIterator($nrit, 0, 7) as $it ) {
    echo '  ', $it, "\n";
  }
  echo "</div>\n";
}
VolkerK
A: 

if(($i-2) % 7 == 0)

sunql