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
Share on other sites

<?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> ";
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.