Jump to content

[SOLVED] Show images from Mysql database if it exists


petenaylor

Recommended Posts

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.

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

 

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!

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>

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>';

}

?>

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.