Show default image if database is null

Posted on 2012-03-27
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

Question by:BrighteyesDesign
LVL 34

Accepted Solution

gr8gonzo earned 500 total points
ID: 37771398
<a href="detail.php?id=<?php echo $row_product['id']; ?>"><?php
  echo '<img src="uploads/thumb_' . $row_product['image'] . '" alt="" border="0" />';
  echo '<img src="" alt="" border="0" />';

Just replace with the correct URL to your own "no image available" image.
LVL 10

Expert Comment

ID: 37771402

Something like that :

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>
	print "No image available";

Open in new window


Author Closing Comment

ID: 37771553
Perfect, thanks!

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Query Using Up Memory 6 43
Incorrect definition of table mysql.proc 7 49
What does this formatting equate to? 7 18
Present Absent from working date rage 11 37
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 …

856 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