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.
2 3345 nathj 938
Recognized Expert Contributor
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: -
// data from a query in an associative array
-
foreach($laNews as $lcNewsItem)
-
{
-
if (empty($lcNewsItem['imageURL'])
-
{
-
$lcNewsItem['imageURL'] = "image/default.png";
-
}
-
echo.... // the line to display the image
-
}
-
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
kovik 1,044
Recognized Expert Top Contributor
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: - echo '<img src="';
-
echo $result->image ? $result->image : 'default.jpg';
-
echo '" />';
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 content type and followed the many postings but no joy.
Here is my code. I'm using 2 pages.
|
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 nothing is displayed in this table.
I have used the code that DMX generates using the server behaviour 'Show
region if recordset is empty', but no success.
Appreciate someone pointing out where I'm going wrong here.
|
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 the querystring for
"rid", and if rid = "" then I do a response.redirect to the page adding the
rid value to the querystring from the recordset.
I'm thinking this is not very effecient, but I can't figure out anything
better.. anyone have a...
|
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 criteria. Then a list box RowSource property is set to
this strSQL, to display the results of the search.
StrSQL is a global variable within this Form's code module, i.e. in
the class module.
|
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 am finding this sort
of problem by far the most annoying part about working with ASP.NET
controls.
What I want is to know how to do three things:
- conditionally show or hide an ImageField +column+ based on whether or not
an the...
| |
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, so it is more like GMAIL's loading bar which is shown just before we land into our mailboxes.
Guys, how do i do it ? Because i have tried ob_start and ob_end_flush, but with no luck i am unable to see the image before script terminates. The whole...
|
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 sorting out a few glitches though.
Since i upgraded from ms access database to MySQL, i have added about 4 articles to test the new setup. I note some fields aren't being added in the new mySql database for the new 4 records. When i ran the MySQK...
|
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 add another field to the query it returns two images. I was making the alt alt=""" & recordset(i) & """ but I know that must be wrong. well it works but returns the image name, ie images/friday.jpg. Anyone know how I can get it to return the image...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |