Solved

Show default image if database is null

Posted on 2012-03-27
3
340 Views
Last Modified: 2012-03-27
I have a product website where my client uploads details ie name, image etc.

If no image is uploaded (which means no filepath entry in the 'image' column of the database) I want to display a generic 'No image available' image. How would I do this?

Here's the code which displays the image...

<a href="detail.php?id=<?php echo $row_product['id']; ?>"><img src="uploads/thumb_<?php echo $row_product['image']; ?>" alt="" border="0" /></a>

Open in new window

0
Comment
Question by:BrighteyesDesign
3 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 37771398
<a href="detail.php?id=<?php echo $row_product['id']; ?>"><?php
if($row_product['image'])
{
  echo '<img src="uploads/thumb_' . $row_product['image'] . '" alt="" border="0" />';
}
else
{
  echo '<img src="http://thegarageblog.com/garage/wp-content/uploads/noimageavailable.jpg" alt="" border="0" />';
}
?></a>

Just replace http://thegarageblog.com/garage/wp-content/uploads/noimageavailable.jpg with the correct URL to your own "no image available" image.
0
 
LVL 10

Expert Comment

by:MadShiva
ID: 37771402
Dear,

Something like that :

<?php
if (empty($row_product['image'])) 
{
?>
<a href="detail.php?id=<?php echo $row_product['id']; ?>"><img src="uploads/thumb_<?php echo $row_product['image']; ?>" alt="" border="0" /></a>
<?php
}
else
	{
	print "No image available";
	}
?>

Open in new window


Regards
0
 

Author Closing Comment

by:BrighteyesDesign
ID: 37771553
Perfect, thanks!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question