0

I am using the following function to display my gallery images:

<?php
//function call
return showImages();

//function definition
function showImages(){
  $out = "<h1>Image Gallery</h1>";
  $out .= "<ul id='images'>";
  $folder = "img";
  $filesInFolder = new DirectoryIterator($folder);
  $imgnumber = 1;
  while ($filesInFolder->valid()){
    $file = $filesInFolder->current();
    $filename = $file->getFilename();
    $src = "$folder/$filename";
    $fileInfo = new Finfo( FILEINFO_MIME_TYPE );
    $mimeType = $fileInfo->file($src);
    if ($mimeType === 'image/jpeg'){
      $out.= "<li><img id='imgnumber$imgnumber' src='$src' /></li>";
    }
    $filesInFolder->next();
    $imgnumber = $imgnumber + 1;
  }

  $out .= "</ul>";
  return $out;
}

The expected result is that img id is incremented by 1 each time the while iteration runs. My problem is that the first image has an id of imgnumber4. This is my result:

<h1>Image Gallery</h1>
<ul id='images'>
  <li><img id='imgnumber4' src='img/1.jpg' /></li>
  <li><img id='imgnumber5' src='img/10.jpg' /></li>
  <li><img id='imgnumber6' src='img/11.jpg' /></li>
  <li><img id='imgnumber7' src='img/12.jpg' /></li>
  <li><img id='imgnumber8' src='img/13.jpg' /></li>
  <li><img id='imgnumber9' src='img/18.jpg' /></li>
  <li><img id='imgnumber10' src='img/2.jpg' /></li>
  <li><img id='imgnumber11' src='img/27.jpg' /></li>
  <li><img id='imgnumber12' src='img/3.jpg' /></li>
  <li><img id='imgnumber13' src='img/4.jpg' /></li>
  <li><img id='imgnumber14' src='img/5.jpg' /></li>
</ul>

Can you tell me why it is starting from number 4 and not 1? I have set the variable $imgnumber to be 1 from start so I have no idea why it jumps to 4...

3
  • 1
    Echo something outside your if block there. Should be obvious. Commented May 8, 2018 at 19:49
  • maybe the first three images don't meet the conditions are therefore are not listed, but it's still counting up? Commented May 8, 2018 at 19:49
  • Ahhh I thought that but all the images from that directory are being displayed. Sorry only starting with PHP and couldn't figure that out... Commented May 8, 2018 at 19:52

1 Answer 1

3

Its your 'if' statement - likely that there are files that are not 'image/jpeg'. Simply place the line

$imgnumber = $imgnumber + 1;

within the if statement

Sign up to request clarification or add additional context in comments.

6 Comments

That worked... I thought that but all the images are being displayed that are located in this folder... Sorry to bother you, only practising :/
Hymm, I only curious now why it jumps to 4 if all the images are meeting the condition of image/jpeg... Thank you once more!
I cannot answer really without seeing all the files in the directory. Are there system files there ?
Only those images from what I can see but only got a new Mac and getting used to it, just like to programming. Coming hard so far and sometimes making myself look like a fool but I will get there! Anyway, it's working as expected now. You saved me hours of frustration haha Thank you once more!
Just saw it is Mac, also .DS_Store.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.