PHP if exists for <div> tag

I have the following code in a script that displays photos with a common name appended with a _01.jpg up to _06.jpg.  The only problem is when the photo does not actually exist..  How do I set this up to only display the <a href> link for these photos only if the .jpg exists?

Can someone edit this code so I can see how to apply this?
<div onclick='dhtmlHistory.add('location1',{message: 'backbox'});countdown()'>
<a href='".$datafd[1][$k+1][0]."$row[0].jpg' rel='lightbox[slide]' caption=''>
<img src='".$datafd[1][$k+1][0]."$row[0].jpg' alt='lime' width='400' height='300' border='0' /></a>
</div>
<a href='".$datafd[1][$k+1][0]."$row[0]_01.jpg' rel='lightbox[slide]' caption=''></a>
<a href='".$datafd[1][$k+1][0]."$row[0]_02.jpg' rel='lightbox[slide]' caption=''></a>
<a href='".$datafd[1][$k+1][0]."$row[0]_03.jpg' rel='lightbox[slide]' caption=''></a>
<a href='".$datafd[1][$k+1][0]."$row[0]_04.jpg' rel='lightbox[slide]' caption=''></a>
<a href='".$datafd[1][$k+1][0]."$row[0]_05.jpg' rel='lightbox[slide]' caption=''></a>
<a href='".$datafd[1][$k+1][0]."$row[0]_06.jpg' rel='lightbox[slide]' caption=''></a>

Open in new window

pda4meAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:
Are the files on the same server as the script? A test equivalent to the below seemed to work for me when that *was* the case.
<div onclick='dhtmlHistory.add('location1',{message: 'backbox'});countdown()'>
<a href='".$datafd[1][$k+1][0]."$row[0].jpg' rel='lightbox[slide]' caption=''>
<img src='".$datafd[1][$k+1][0]."$row[0].jpg' alt='lime' width='400' height='300' border='0' /></a>
</div>
<?php
for($i=1;$i<=6;$i++) {
  if (is_file("{$datafd[1][$k+1][0]}{$row[0]}_0$i.jpg")) {
    print "<a href='{$datafd[1][$k+1][0]}{$row[0]}_0$i.jpg' rel='lightbox[slide]' caption=''></a>";
  }
}
?>

Open in new window

0
pda4meAuthor Commented:
Yes, they are local to the server as well.   I am having trouble with the use of " as opposed to ' in your script and can't seem to figure it out?  I noticed its very picky when I use one versus the other as it took awhile to get it right in my snippet.  Can you take a review of your script and see what I am doing wrong?
0
Terry WoodsIT GuruCommented:
Variables within " are evaluated, but they are not when they are within ' - eg

$hello = "world";
print "$hello\n"; #outputs world
print '$hello\n'; #outputs $hello\n (even the \n isn't evaluated)

When you use array variables such as $datafd[1][$k+1][0] you don't need to end the " to append them as long as they are surrounded in curly braces {} - eg:

$mello = array("yello"=>"cello");
#print "$mello['yello']\n"; #gives a syntax error
print "{$mello['yello']}\n"; #outputs cello
print '$mello["yello"]\n'; #outputs $mello["yello"]\n
#print '$mello['yello']\n'; #gives a syntax error as the single quotes are terminated after the [ and
                                    #php doesn't know what to do with what comes after that
#print '{$mello['yello']}\n'; #gives a syntax error as the single quotes are terminated after the [ and
                                        #php doesn't know what to do with what comes after that

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Terry WoodsIT GuruCommented:
Perhaps I should focus more on the top part of the code now, as it won't actually work the way you (or I) showed it. There's a few ways of ways of putting variables into html code.

print "<a href='{$example[0]}.jpg'>"; #this has to go within <?php ?> tags

<a href='<?=$example[0]?>.jpg'> #this goes outside of php tags, and uses special php tag syntax to just print the contents of the variable

<a href='<? print $example[0]; ?>.jpg'> #this goes outside of php tags, and uses standard php tag syntax to print the contents of the variable

print <<<BOB
<a href='{$example[0]}.jpg'>
<p>Another line of html code</p>
BOB;   #BOB can be a string of your choice, as I understand it. Anyway, this is similar to the first example above but is good when you have multiple lines to print.

I'd probably do it this way:
<?php
print <<<BOB
<div onclick='dhtmlHistory.add('location1',{message: 'backbox'});countdown()'>
<a href='{$datafd[1][$k+1][0]}{$row[0]}.jpg' rel='lightbox[slide]' caption=''>
<img src='{$datafd[1][$k+1][0]}{$row[0]}.jpg' alt='lime' width='400' height='300' border='0' /></a>
</div>
BOB;
for($i=1;$i<=6;$i++) {
  if (is_file("{$datafd[1][$k+1][0]}{$row[0]}_0$i.jpg")) {
    print "<a href='{$datafd[1][$k+1][0]}{$row[0]}_0$i.jpg' rel='lightbox[slide]' caption=''></a>";
  }
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Terry WoodsIT GuruCommented:
My examples are very similar to what's on this page actually:
http://jp2.php.net/manual/en/function.print.php
0
pda4meAuthor Commented:
thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.