html - How to make this PHP code run more efficiently? -
php code:
$automaticallyopenshow = $automaticallyopen."/"; $images = scandir($automaticallyopen,0); $counter = 0; foreach($images $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 kurro1 , raggamuffin-420 answer. made the integration
php code:
$automaticallyopenshow = $automaticallyopen."/"; $images = scandir($automaticallyopen,0); $counter = 1; foreach($images $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" />
how using array storage instead of 4 variables?:
$automaticallyopenshow = $automaticallyopen."/"; $images = scandir($automaticallyopen,0); $counter = 0; $imageview = array(); foreach($images $curimg) { // condition optimized if (strpos($curimg, '.jpg')>0 || strpos($curimg, '.jpg')>0) { // write result in array @ appropriate position $imageview[$counter++] = $automaticallyopenshow.$curimg; } }
the html code following:
<img src="<?php echo $imageview[0]; ?>" width="500" height="500" />
Comments
Post a Comment