spacitron
spacitron

Reputation: 459

Dealing with infinite loops

I'm trying to learn PHP programming through trial, error and lots and lots of practice. I've been working on a directory bases image gallery for the last few days and it seems to work fine. I can upload pictures and create new albums. Plus whenever I see a full size picture I have links to the previous and next pictures in the directory.

Now this is where I ran into problems. Once you got to the end of the directory the code would start an infinite loop while it searches for the next file, which isn't there.

So I altered my code and it now looks something like this:

$x = 1;
$dir = opendir($base.'/'.$get_album);
while ((($file = readdir($dir)) !== FALSE) && ($x == 1)) {
  while ($img < $file) {
    $img++;
    $image_exists = file_exists($base.'/'.$get_album.'/'.$img);
    if ($image_exists) {
      echo "<a href='member.php?album=$get_album&imgID=$img'>
              <img src='$base/$get_album/$img' width='70' align='right'>
            </a>";
      break;
    }
    $x++;
  }
}

This works when I get to the last picture in the directory. So I thought I'd do the same for when I get to the first picture and just invert the operators like so:

$x = 1;
$dir = opendir($base.'/'.$get_album);
while ((($file = readdir($dir)) !== FALSE) && ($x == 1)) {
  while ($img > $file) {
    $img--;
    $image_exists = file_exists($base.'/'.$get_album.'/'.$img);
    if ($image_exists) {
      echo "<a href='member.php?album=$get_album&imgID=$img'>
              <img src='$base/$get_album/$img' width='70' align='right'>
            </a>";
      break;
    }
    $x++;
  }
}

This however DOES NOT work. I can't understand why that would be the case and I've tried to change things around a few dozen times but I cant seem to make it stop looping.

What am I doing wrong?

Thanks in advance for your help (and for reading through all this).

Upvotes: 0

Views: 184

Answers (3)

Matt Esch
Matt Esch

Reputation: 22956

The issue could likely be related to the fact that readdir returns a string, and you are treating it as if you know its a number. You might find that it is returning you something at the start or end, such as the parent directory '..' as a string or the current directory as a string '.'.

If you want to iterate over the directories, you should check that the return value from readdir is not '.' or '..' specifically.

Upvotes: 1

Igor Parra
Igor Parra

Reputation: 10348

Please use a better format to your code (it's really painful read it), for instance, in netbeans you can use shiftalt F to format automatically.

I want address your attention to this line:

$image_exists= file_exists($base.'/'.$get_album.'/'.$img);

That is a possible source of errors. If only $base.'/'.$get_album.'/' exists then:

$image_exists= file_exists($base.'/'.$get_album.'/'.$img); // is true even if $img is empty!!!

But moreover:

What is $img? You treat it as a number when do $img++ and then as a string. That is not good for your health.

A good start it should be initialize your vars at the beginning of your code. And then use them in consonance.

Upvotes: 1

jeroen
jeroen

Reputation: 91734

I think there is something fundamentally wrong with the design, you are comparing $img, an uninitialized integer, to $file, a string.

It also seems needlessly complicated, as you are reading files from a directory, then check if they exist and break out of your loop if they do.

Perhaps with a clear view of the directory structure and what you are trying to do, I could provide a more detailed answer, but I think you need to go back to the drawing board.

Upvotes: 0

Related Questions