0

After having a solution to this question.

my code:-

<?php for ($i = 1; $i <= 9; $i++):?>
<div class="col-md-2">
  <div class="thumbnail">
    <a href="<?php echo $details . $i;?>.php">
      <img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
    </a>
  </div>
</div>
<?php endfor; ?>    

So now assume that there is now image as 8.png, so for loop should skip that and go to next, without showing no image.

Result is. And I want this

The red Portion should not be displayed.

Chirag Jain
  • 1,367
  • 1
  • 11
  • 27

5 Answers5

2

Since you stated in the comments that the URL to the images is localhost/downloads/vf/images, I will assume that with usage of $_SERVER['DOCUMENT_ROOT'] takes care of the folders outside public (which could be something like C:\users\chirag\htdocs\, but that $_SERVER variable should take care of that. If that's not the case, find the full path and use that instead - as file_exists() requires the system path, not the public path/URL. This also assumes that you use a relative path for the $images variable, which means that you have $images = "/downloads/vf/images/"; and not $images = "localhost/downloads/vf/images/";.

With that assumption, we can now use file_exists() - because that takes the system path, not the URL.

<?php 
$images = "/downloads/vf/images/";
for ($i = 1; $i <= 9; $i++):
    if (!file_exists($_SERVER['DOCUMENT_ROOT'].$images.$i.".png"))
        continue; // Skip this iteration if it can't find the file 
    ?>
    <div class="col-md-2">
      <div class="thumbnail">
        <a href="<?php echo $details . $i;?>.php">
          <img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
        </a>
      </div>
    </div>
<?php endfor; ?>   
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • `$images` should just be the relative path, so it should be `/downloads/vf/images/` and not the full path (`localhost/downloads/vf/images`). – Qirel Sep 02 '17 at 09:52
0

You will have to add a check condition if the image exists or not, there are certain methods like file_exists

<?php for ($i = 1; $i <= 9; $i++): 
    if(file_exists("file_path")){ ?>
      <img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
<? { php endfor; ?>

This might help you: How to check if image exists

bhansa
  • 7,282
  • 3
  • 30
  • 55
0

Use this code

File_exists is function check for the file exists or not

<?php for ($i = 1; $i <= 9; $i++): 
   if (file_exists($images.$i.'.png')) { ?>
        <img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">

<?php } endfor; ?>

check this like for more detail

PHP: How to check if image file exists?

Himanshu sharma
  • 7,487
  • 4
  • 42
  • 75
-1

You need to use the continue directive. I also suggest you drop the use of : and endfor and all the extra PHP start/end blocks for readability and performance reasons.

<?php
  for ($i = 1; $i <= 9; $i++) {
    if (!is_readable($images . $i . '.png'))
      continue;

    echo "<img src=\"${images}${i}.png\" alt=\"$i\">";
  }
?>
HostFission
  • 374
  • 1
  • 7
  • it is not necessary that always there will no 8.png. it may also be that it doesn't have 4.png.. – Chirag Jain Sep 02 '17 at 09:04
  • I have div also with them... Also see https://stackoverflow.com/questions/46002338/auto-increasing-number-in-img-src before editing it! – Chirag Jain Sep 02 '17 at 09:19
  • `is_readable` checks for the file on disk... this will skip any file that is missing provided the path presented to `is_readable` is correct. – HostFission Sep 02 '17 at 09:44
-1

If you use the keyword continue inside any loop, the rest of the iteration after that would be skipped.

<?php for ($i = 1; $i <= 9; $i++): ?>

      <?php if ($i == 8) continue;>

      <img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
<?php endfor; ?>

This keyword is common for a lot of languages.

Also in PHP, the continue accepts a numeric argument which tells it how many iterations it should skip.

PHP continue

Charlie
  • 22,886
  • 11
  • 59
  • 90
  • It's not my downvote, but OP said in a comment above that it might not necessarily be 8.png that's missing, it might be 4.png - but again, not my downvote. Oh, and it appears that you have a slight syntax error – Qirel Sep 02 '17 at 09:17