开发者

Looping through a directory in PHP?

开发者 https://www.devze.com 2023-02-20 15:43 出处:网络
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>

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' r开发者_开发技巧el='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


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


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.


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"


$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);
    }
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消