Jump to content

display image only if it exists?


willingtolearn

Recommended Posts

hello - i am working on a script that someone else has created and need to add the option for more images to be displayed - only if there is any..

the script is the same for all images 1-6, I have copied and pasted for the additional images but i only want them to display only if there is any added... how do i edit the below code to show this

I am new to php, so please be kind - any help you can give would be greatly appreciated.

 

?>

$query = "SELECT photo_name FROM photos WHERE PID=$id ORDER BY photo_name ASC LIMIT 5, 1";

$photos = mysql_query($query) or die ("Select Failed!");

$photo6 = mysql_fetch_array($photos);

$photo6 = $photo6['photo_name'];

?>

<a href="../smallphotos/<? echo $photo6; ?>.jpg" rel="lightbox[Villas]" >

<img src="../smallphotos/<? echo $photo6; ?>.jpg" width="280" height="210" border="1">

</a>

Link to comment
Share on other sites

Add if condition before the image link tag, check the below code

 

<?php if (file_exists("../smallphotos/".$photo6.".jpg")) { ?>
<a href="../smallphotos/<? echo $photo6; ?>.jpg" rel="lightbox[Villas]" >
<img src="../smallphotos/<? echo $photo6; ?>.jpg" width="280" height="210" border="1">
</a>
<?php } ?>

Link to comment
Share on other sites

thanks so much for replying - i really really appreciate it!!

 

well, my head is exploding.. it doesn't work:-(

 

i keep getting an error :

Parse error: syntax error, unexpected '<' in /home/morairal/public_html/rentals/details100.php on line 219

which is:

<?php if (file_exists("../smallphotos/".$photo7.".jpg")) { ?>

 

the page link is http://www.morairaluxuryvillas.com/rentals/details100.php?id=45

 

<?

$query = "SELECT photo_name FROM photos WHERE PID=$id ORDER BY photo_name ASC LIMIT 4, 1";

$photos = mysql_query($query) or die ("Select Failed!");

$photo7 = mysql_fetch_array($photos);

$photo7 = $photo7['photo_name'];

 

<?php if (file_exists("../smallphotos/".$photo7.".jpg")) { ?>

<a href="../smallphotos/<? echo $photo7; ?>.jpg" rel="lightbox[Villas]" >

<img src="../smallphotos/<? echo $photo7; ?>.jpg" width="280" height="210" border="1">

</a>

<?php } ?>

Link to comment
Share on other sites

Assuming the code appears as described above, there's an extra "<?php".

 

$photo7 = $photo7['photo_name'];
            
<?php if (file_exists("../smallphotos/".$photo7.".jpg")) { ?>

 

 

Should be

 

$photo7 = $photo7['photo_name'];
            
if (file_exists("../smallphotos/".$photo7.".jpg")) { ?>

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.