Matt Elhotiby
Matt Elhotiby

Reputation: 44066

Looping through a directory in PHP?

I am trying to print print this out in the html

 <a id='riverwalk' style='display:none;' href='images/riverwalk/1.jpg' rel='prettyPhoto[riverwalk]'></a>
<a style='display:none;' href='images/riverwalk/2.jpg' rel='prettyPhoto[riverwalk]'></a>
<a style='display:none;' href='images/riverwalk/3.jpg' rel='prettyPhoto[riverwalk]'></a>
<a style='display:none;' href='images/riverwalk/4.jpg' rel='prettyPhoto[riverwalk]'></a>

But i want to do it pro grammatically and this is my code..but just not doing anything at all

$counter = 0;
$directory = "images/riverwalk"
$dir = opendir ("images/riverwalk");
while (false !== ($file = readdir($dir))) {
        if ($counter == 0) {
            echo "<a id='riverwalk' style='display:none;' href='images/riverwalk/$file' rel='prettyPhoto[riverwalk]'></a>";
        $counter++;
        }else{
         echo "<a style='display:none;' href='images/riverwalk/$file' rel='prettyPhoto[riverwalk]'></a>";
    $counter++;
}
}

does anything look off to anyone...i cant view the errors because i cant turn php errors on...any ideas

FIX: the issue was the a semicolon missing on the second line

Upvotes: 1

Views: 4333

Answers (4)

contrid
contrid

Reputation: 980

$dir = './'; open_dir($dir);

function open_dir($dir = null) { global $htaccesscount;

if (!empty($dir) && is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if ($file != "." && $file != "..") {
                $filetype = filetype($dir . $file);

                switch ($filetype) {
                    case 'dir'          :

                        break;
                    case 'file'         :

                        break;
                }
            }
        }
        closedir($dh);
    }
}

}

Upvotes: 0

RichW
RichW

Reputation: 10912

Take a look at scandir() - it's much easier to write and debug..

$counter = 0;
$dirf    = 'images/riverwalk';
$dir = scandir($dirf);
foreach($dir as $file) {
   if(($file!='..') && ($file!='.')) {
      if($counter==0)
           echo "<a id='riverwalk' style='display:none;' href='images/riverwalk/$file' rel='prettyPhoto[riverwalk]'></a>";
      else
           echo "<a style='display:none;' href='images/riverwalk/$file' rel='prettyPhoto[riverwalk]'></a>";
      $counter++;
   }
}

If that doesn't work use is_dir() to check you're pointing at the correct directory..

Upvotes: 2

Wh1T3h4Ck5
Wh1T3h4Ck5

Reputation: 8509

Maybe you missed semicolon (;) in line 2 of your PHP code... Try to add ; at the and of line. This code works fine!

consider this line:

$directory = "images/riverwalk"

Upvotes: 1

Martin Hennings
Martin Hennings

Reputation: 16846

I hope I don't go too far by saying: It actually does work, but you can't see anything because your <a>'s are empty. Try it with

    if ($counter == 0) {
        echo "<a id='riverwalk' style='display:none;' href='images/riverwalk/$file' rel='prettyPhoto[riverwalk]'> blib </a>";
    $counter++;
    }else{
     echo "<a style='display:none;' href='images/riverwalk/$file' rel='prettyPhoto[riverwalk]'> blub </a>";

(added 'blib' and 'blub')

If you did think of this (in that case please provide html source code in your question), tell me in a comment and I'll delete this answer.

Upvotes: 1

Related Questions