Solved

Show default image if database is null

Posted on 2012-03-27
3
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Tobias
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 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