Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP if exists for <div> tag

Posted on 2008-11-05
6
Medium Priority
?
882 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:pda4me
  • 4
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22892234
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
 

Author Comment

by:pda4me
ID: 22893518
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22899201
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 22899320
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22899375
My examples are very similar to what's on this page actually:
http://jp2.php.net/manual/en/function.print.php
0
 

Author Closing Comment

by:pda4me
ID: 31513771
thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question