willingtolearn Posted May 25, 2012 Share Posted May 25, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/263106-display-image-only-if-it-exists/ Share on other sites More sharing options...
QuickOldCar Posted May 25, 2012 Share Posted May 25, 2012 http://php.net/manual/en/function.file-exists.php Quote Link to comment https://forums.phpfreaks.com/topic/263106-display-image-only-if-it-exists/#findComment-1348532 Share on other sites More sharing options...
PravinS Posted May 25, 2012 Share Posted May 25, 2012 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263106-display-image-only-if-it-exists/#findComment-1348533 Share on other sites More sharing options...
willingtolearn Posted May 25, 2012 Author Share Posted May 25, 2012 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263106-display-image-only-if-it-exists/#findComment-1348562 Share on other sites More sharing options...
cyberRobot Posted May 25, 2012 Share Posted May 25, 2012 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")) { ?> Quote Link to comment https://forums.phpfreaks.com/topic/263106-display-image-only-if-it-exists/#findComment-1348568 Share on other sites More sharing options...
willingtolearn Posted May 25, 2012 Author Share Posted May 25, 2012 if i could kiss u i would thank u thank u thank u !! Quote Link to comment https://forums.phpfreaks.com/topic/263106-display-image-only-if-it-exists/#findComment-1348585 Share on other sites More sharing options...
cyberRobot Posted May 25, 2012 Share Posted May 25, 2012 if i could kiss u i would Hmm... Glad to help! Quote Link to comment https://forums.phpfreaks.com/topic/263106-display-image-only-if-it-exists/#findComment-1348591 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.