Question

PHP code:

$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 0;
foreach($images as $curimg) {
        if (strpos($curimg, '.jpg')>0 || strpos($curimg, '.JPG')>0) {

                if($counter==1){$ImageView_1 = $AutomaticallyOpenShow.$curimg; }
            elseif($counter==2){$ImageView_2 = $AutomaticallyOpenShow.$curimg; }
            elseif($counter==3){$ImageView_3 = $AutomaticallyOpenShow.$curimg; }
            elseif($counter==4){$ImageView_4 = $AutomaticallyOpenShow.$curimg; }


        $counter++;
}
}   

HTML code:

<img src="<?php echo ImageView_1 ; ?>" width="500" height="500" />

Thanks for Kurro1 and RaggaMuffin-420 answer. I finally made ​​the integration

PHP code:

$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 1;
foreach($images as $curimg) {

  if (preg_match('/^.*\.[jpeg]{3,4}$/i', $curimg)) {

$ImageView[$counter++] = $AutomaticallyOpenShow.$curimg;

$counter++;
  }
}

HTML code:

<img src="<?php echo $ImageView[????] ; ?>" width="500" height="500" />
Was it helpful?

Solution

How about using an array for storage instead of 4 variables?:

$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 0;
$ImageView = array();
foreach($images as $curimg) {
        // the condition could also be optimized
        if (strpos($curimg, '.jpg')>0 || strpos($curimg, '.JPG')>0) {
            // write result in array at appropriate position
            $ImageView[$counter++] = $AutomaticallyOpenShow.$curimg;
        }

}   

The html code would be like the following:

<img src="<?php echo $ImageView[0]; ?>" width="500" height="500" />

OTHER TIPS

$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 1;
foreach($images as $curimg) {
  if (preg_match('/^.*\.[jpeg]{3,4}$/i', $curimg)) {

    if($counter >= 1 && $counter <= 4){
      $varName = 'ImageView_'.$counter;
      $$varName = $AutomaticallyOpenShow.$curimg;
    }

    $counter++;
  }
}

Based on the first code you posted.

PHP code:

$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$ImageView = Array();
$counter = 0;

foreach($images as $curimg) {
        if (strpos($curimg, '.jpg')>0 || strpos($curimg, '.JPG')>0) {
            $ImageView[$counter++] = $AutomaticallyOpenShow.$curimg;
    }
}   

HTML code:

<img src="<?php echo ImageView[0] ; ?>" width="500" height="500" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top