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
https://forums.phpfreaks.com/topic/263106-display-image-only-if-it-exists/
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 } ?>

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

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")) { ?>

Archived

This topic is now archived and is closed to further replies.

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