petenaylor Posted June 16, 2009 Share Posted June 16, 2009 Hi all I am trying to create a page that shows the images from an SQL database if they exist. I don't want it to show an image at all if there isn't one in the database. Here's my code: <a href="images/parts/<?php echo $row_rs_radiators['image']; ?>" target="_blank"> <img src="images/parts/<?php echo $row_rs_radiators['image']; ?>" alt="<?php echo $row_rs_radiators['title']; ?>" width="200" /></a> This brings the image from the database. There are image, image2, image3 and image4 fields in the SQL database but if there aren't any images in 'image2' 'image3' or 'image4' I don't want it to display an image. If there is an image in those fields I want it to display them. This is the code I have to stop it displaying the image is there isn't one: <?php if (empty($row_rs_radiators['image2'])) { echo '';} ?> How do I write the 'else' code to show the image if there is one? Many thanks Pete. Link to comment https://forums.phpfreaks.com/topic/162369-solved-show-images-from-mysql-database-if-it-exists/ Share on other sites More sharing options...
joel24 Posted June 16, 2009 Share Posted June 16, 2009 <?php if (!empty($row_rs_radiators['image2'])) { echo $row_rs_radiators['image2']; } ?> just need to put a ! infront of the empty to get the opposite value...? i.e. if its not empty. also are your images stored in the database, or just a link to the image? cause if its stored in the database you're going to get php echoing a load of binary data and no image will be displayed.... Link to comment https://forums.phpfreaks.com/topic/162369-solved-show-images-from-mysql-database-if-it-exists/#findComment-856988 Share on other sites More sharing options...
petenaylor Posted June 16, 2009 Author Share Posted June 16, 2009 Great! That works, the only problem is that the image is stored as 'image2.jpg' in my sql database but the url prefix is 'images/parts/' which is in the php code and not the SQL entry. How can I add this into the code above? Thanks! Link to comment https://forums.phpfreaks.com/topic/162369-solved-show-images-from-mysql-database-if-it-exists/#findComment-856994 Share on other sites More sharing options...
joel24 Posted June 16, 2009 Share Posted June 16, 2009 <?php if (!empty($row_rs_radiators['image2'])) { echo 'images/parts'.$row_rs_radiators['image2']; } ?> Link to comment https://forums.phpfreaks.com/topic/162369-solved-show-images-from-mysql-database-if-it-exists/#findComment-856997 Share on other sites More sharing options...
petenaylor Posted June 16, 2009 Author Share Posted June 16, 2009 That's almost it, but that shows the actual image url . I need to add in '<img src>' so it shows the image. Can this be added into this code? Thanks for your help! Link to comment https://forums.phpfreaks.com/topic/162369-solved-show-images-from-mysql-database-if-it-exists/#findComment-857004 Share on other sites More sharing options...
joel24 Posted June 16, 2009 Share Posted June 16, 2009 going from your original post... <a href="images/parts/<?php if (!empty($row_rs_radiators['image2'])) { echo $row_rs_radiators['image2']; } ?>" target="_blank"> <img src="images/parts/<?php if (!empty($row_rs_radiators['image2'])) { echo $row_rs_radiators['image2']; } ?>" alt="<?php echo $row_rs_radiators['title']; ?>" width="200" /></a> Link to comment https://forums.phpfreaks.com/topic/162369-solved-show-images-from-mysql-database-if-it-exists/#findComment-857006 Share on other sites More sharing options...
joel24 Posted June 16, 2009 Share Posted June 16, 2009 wooops. that will just put in the image src="images/parts/" if the database image field is empty. use this.. <?php if (!empty($row_rs_radiators['image2'])) { echo '<a href="images/parts/'.$row_rs_radiators['image2'].'" target="_blank">'; echo '<img src="images/parts/'.$row_rs_radiators['image2'].'" alt="'.$row_rs_radiators['title'].'" width="200" /></a>'; } ?> Link to comment https://forums.phpfreaks.com/topic/162369-solved-show-images-from-mysql-database-if-it-exists/#findComment-857007 Share on other sites More sharing options...
petenaylor Posted June 16, 2009 Author Share Posted June 16, 2009 That works perfectly! Thanks Joel! Link to comment https://forums.phpfreaks.com/topic/162369-solved-show-images-from-mysql-database-if-it-exists/#findComment-857010 Share on other sites More sharing options...
joel24 Posted June 16, 2009 Share Posted June 16, 2009 click solved down the bottom left Link to comment https://forums.phpfreaks.com/topic/162369-solved-show-images-from-mysql-database-if-it-exists/#findComment-857013 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.