Jump to content

Better way to write this?


eMonk

Recommended Posts

if (file_exists("images/".$id."/img01.jpg")) { echo "<a href=\"/v1/images/".$id."/img01.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb01.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; }
if (file_exists("images/".$id."/img02.jpg")) { echo "<a href=\"/v1/images/".$id."/img02.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb02.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; }
if (file_exists("images/".$id."/img03.jpg")) { echo "<a href=\"/v1/images/".$id."/img03.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb03.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; }
if (file_exists("images/".$id."/img04.jpg")) { echo "<a href=\"/v1/images/".$id."/img04.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb04.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; }
if (file_exists("images/".$id."/img05.jpg")) { echo "<a href=\"/v1/images/".$id."/img05.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb05.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; }
if (file_exists("images/".$id."/img06.jpg")) { echo "<a href=\"/v1/images/".$id."/img06.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb06.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; }
if (file_exists("images/".$id."/img07.jpg")) { echo "<a href=\"/v1/images/".$id."/img07.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb07.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; }
if (file_exists("images/".$id."/img08.jpg")) { echo "<a href=\"/v1/images/".$id."/img08.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb08.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; }
if (file_exists("images/".$id."/img09.jpg")) { echo "<a href=\"/v1/images/".$id."/img09.jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb09.jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a>"; }

 

This code works but it just seems a bit tedious. I may also include up to 20 images to check if they exist. Better way to do this?

Link to comment
https://forums.phpfreaks.com/topic/255958-better-way-to-write-this/
Share on other sites

for($i = 0; $i < 0; $i++){

 

  if (file_exists("images/".$id."/img0".$i .".jpg")) { echo "<a href=\"/v1/images/".$id."/img0".$i .".jpg\" rel=\"lightbox\"><img src=\"/v1/images/".$id."/thumb0".$i .".jpg\" class=\"img\" border=\"0\" width=\"60\" height=\"60\"></a> "; }

 

}

 

 

<?php
$files = glob("images/$id/img*.jpg"); // get a list of images
if(empty($files)){
echo "No matching images!";
} else {
foreach($files as $file){
	$thumb = str_replace('img','thumb',$file); // produce thumb image name
	echo "<a href='/v1/$file' rel='lightbox'><img src='/v1/$thumb' class='img' border='0' width='60' height='60'></a> ";
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.