473,413 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Show default image if recordset is empty

Can you use PHP to show a default image if the image field in a database row is empty?

I'm creating a news page, but the client may not always upload an image with a news story. I know how to hide the area, but this leaves a big blank space. It would be much better if a default image were to appear.
Jul 26 '07 #1
2 3314
nathj
938 Expert 512MB
Can you use PHP to show a default image if the image field in a database row is empty?

I'm creating a news page, but the client may not always upload an image with a news story. I know how to hide the area, but this leaves a big blank space. It would be much better if a default image were to appear.
There are a coule of solutions.

1) When they load the news item you could fill in the form with a default if they don't supply one so the default value is a link to the default image. this could be a simple as a default value in the table so would be really simple. Or you could code the default as the data goes into the table.

2) Have the HTML so that if there is no image the div or whatever does not display

3) As you loop through the data building the news page use an IF statement. If the image field is not empty then display it else dosplay default eg:
Expand|Select|Wrap|Line Numbers
  1. // data from a query in an associative array
  2. foreach($laNews as $lcNewsItem)
  3. {
  4.    if (empty($lcNewsItem['imageURL'])
  5.    {
  6.      $lcNewsItem['imageURL'] = "image/default.png";
  7.     }
  8.    echo.... // the line to display the image
  9. }
  10.  
I would opt for either option one or three. But, as always there are many solutiosn to one problem and you have to pick which is best for you. Also I'm sure other people will post other options.

Hopefully that's got you going.

Cheers
nathj
Jul 26 '07 #2
kovik
1,044 Expert 1GB
This is something that many websites do, such as this one when people don't specify avatars. The simplest way to do it is a one-line ternary statement:

Expand|Select|Wrap|Line Numbers
  1. echo '<img src="';
  2. echo $result->image ? $result->image : 'default.jpg';
  3. echo '" />';
Jul 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bob | last post by:
Hello: I've tried showing an image field from an MS SQL Server 2000 image field on an ASP page but All I get is this http://http://new.confectioneryhouse.com/glutenfree/about.asp I added the...
5
by: David Ehmer | last post by:
The code below is 2 rows in a table, the top row contains a message to be shown if the recordset returns no matches. The 2nd row will display any matches. Problem is that if no matches are found...
3
by: Bryan Harrington | last post by:
I have a list of requests that I'm displaying. The client wants to be able to display the details of the FIRST request as the page loads. Right now, as I loop through the recordset, I'm checking...
7
by: Jean | last post by:
Hello, I have a form that performs a search, according to criteria that a user enters into the text boxes. When the user clicks on „Search", a SQL string (say strSQL) is built up with the...
3
by: | last post by:
I'm using the DataList and GridView controls, and I am trying to wrap my head around the problem of conditionally showing or hiding cells/cell content based on the presence or absence of DB data. I...
20
realin
by: realin | last post by:
hiya guys, I have a question, i want to display the image which is above the page, while my script loads in the background. This is a simple script which check for new messages in my mailbox,...
3
by: janetopps | last post by:
I have a news website, with asp pages, which was on Access, and i upgraded to MySQL, i used Bullzip to transfer the data. It had about 1000 pages, which im now able to pull up on the public side. Im...
3
by: fran7 | last post by:
Hi, I have this nice code that returns a random image database record. It works great. What I am trying to do now is to be able to get the "alt" description for the image from another field. If I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.