tags:

views:

59

answers:

5
+3  A: 

Assuming that all files in the promos directory are images:

<div class="scrollable" id="browsable">   
   <div class="items"> 
      <?php 
        if ($handle = opendir('./promos/')) {

            while (false !== ($file = readdir($handle))) {
                echo "<div>";
                    echo "<a href='#'><img src='".$file."' /></a>";
                echo "</div>";
            }
            closedir($handle);
        }
      ?>
    </div>
</div>

If, however, there are files in the directory that are not images, you would need to check that before showing it. The while loop would need to change to something like:

while (false !== ($file = readdir($handle))) {
    if ((strpos($file, ".jpg")) || (strpos($file, ".gif"))) {
        echo "<div>";
            echo "<a href='#'><img src='".$file."' /></a>";
        echo "</div>";
    }
}
JGB146
A: 

Go thou unto http://www.php.net/manual/en/ref.dir.php and look in particular at the scandir function. You can use something like:

$images = array();
foreach (scandir('somewhere') as $filename)
    if (is_an_image($filename)) $images[] = $filename;

You get to write the is_an_image() function.

Ian
A: 

My way to do it with opendir

<div class="scrollable" id="browsable">   
<div class="items"> 

<?php
    $c=0;
    if ($handle = opendir('promo')) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                $images[$c] = $file;
                $c++;
            }
        }
        closedir($handle);
    }

    for ($counter = 0; $counter <= count($images); $counter ++) {
        echo "<div>";
            echo '<a href="#"><img src="'.$image[$counter].'" /></a>';
        echo "</div>";
    }
          ?>
    </div>
</div>
Mark
A: 

How about the DirectoryIterator class?

<div class="scrollable" id="browsable">   
    <div class="items"> 
        <?php foreach (new DirectoryIterator('folder/goes/here') as $file): ?>
        <?php if($file->isDot || !$file->isReadable()) continue; ?>

            <div><a href="#"><img src="filepath/<?php echo $file->getFilename(); ?>" /></a></div>

          <?php endforeach; ?>
    </div>
</div>
Kieran Allen
Also using iterators, have a look at this question: http://stackoverflow.com/questions/3321547/help-using-regexiterator-in-php
Chris
Here goes spaghetti...
Col. Shrapnel
A: 

I answered a very similar question here: http://stackoverflow.com/questions/3275718/is-there-a-way-to-put-this-php-into-an-array-and-simplify-it/3278243#3278243

It should be what you need.

Gipetto