eMonk Posted January 29, 2012 Share Posted January 29, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255958-better-way-to-write-this/ Share on other sites More sharing options...
trq Posted January 29, 2012 Share Posted January 29, 2012 Have you heard of loops? Quote Link to comment https://forums.phpfreaks.com/topic/255958-better-way-to-write-this/#findComment-1312090 Share on other sites More sharing options...
Ivan Ivković Posted January 29, 2012 Share Posted January 29, 2012 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> "; } } Quote Link to comment https://forums.phpfreaks.com/topic/255958-better-way-to-write-this/#findComment-1312101 Share on other sites More sharing options...
PFMaBiSmAd Posted January 29, 2012 Share Posted January 29, 2012 <?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> "; } } Quote Link to comment https://forums.phpfreaks.com/topic/255958-better-way-to-write-this/#findComment-1312104 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.