Is it possible to set an "if" condition to check if image is available then display it?
I am loading images from an url that will have the ID loaded from the database. Image URL links with bad id will show like this: Example
now on the page it will display as the small blank box, how can i make it display something else or hide it all together?
This is my code - <a href="http://us.playstation.com/publictrophy/index.htm?onlinename={vb:raw userinfo.field5}">
-
<img src="http://fp.profiles.us.playstation.com/playstation/psn/pid/{vb:raw userinfo.field5}.png">
-
</a><br /><br />
-
Thanks
7 2469 Atli 5,058
Expert 4TB
Hey.
If the image you are loading does not exist, or the server refuses to serve it to you, it is usually safe to assume the server response includes a header to reflect that. You can use the get_headers function to fetch the headers for your image, and check that to see if the server is willing to serve you the image. You can then, based on that, either try to use the image or serve alternate content.
For example, consider this: - <?php
-
// The URL to the image you want to
-
// use in your code.
-
$imgUrl = "http://fp.profiles.us.playstation.com/playstation/psn/pid/wrongurlexample.png";
-
-
// Fetch the response headers the server
-
// sends when the image is requested.
-
$headers = get_headers($imgUrl);
-
-
// See if the first line includes 200,
-
// which is the HTTP response code for success.
-
if(stristr($headers[0], "200"))
-
{
-
// The server can successfully serve
-
// the image, so we can use it!
-
echo "<img src=\"{$imgUrl}\" alt=\"My Image\">";
-
}
-
else {
-
// The server can not serve the image.
-
// Do whatever you want to do instead,
-
// like serve an error image.
-
echo '<img src="error.png" alt="An error occured">';
-
}
-
?>
@Atli
Hey,
Thank you for your response. This is the code I implemented for my page - <?php
-
// The URL to the image you want to
-
// use in your code.
-
$imgUrl = "http://fp.profiles.us.playstation.com/playstation/psn/pid/{vb:raw userinfo.field5}.png";
-
-
// Fetch the response headers the server
-
// sends when the image is requested.
-
$headers = get_headers($imgUrl);
-
-
// See if the first line includes 200,
-
// which is the HTTP response code for success.
-
if(stristr($headers[0], "200"))
-
{
-
// The server can successfully serve
-
// the image, so we can use it!
-
echo '<a href="http://us.playstation.com/publictrophy/index.htm?onlinename={vb:raw userinfo.field5}"><img src=\"{$imgUrl}\"></a>';
-
};
-
else {
-
// The server can not serve the image.
-
// Do whatever you want to do instead,
-
// like serve an error image.
-
echo '<a href="http://us.playstation.com/publictrophy/index.htm?onlinename={vb:raw userinfo.field5}"><img src="cpstyles/darkvision/misc/nopid.png"></a>';
-
}
-
?>
-
Now everything disappeared. Theres absolutely nothing displaying.
What seems to be the problem and how do i fix it?
Thanks
Atli 5,058
Expert 4TB
Do you have the debug messages turned on?
I'm guessing the problem is the extra semi-colon on line #17. It could also be the value you insert into the URLs: "{vb:raw userinfo.field5}". I'm not sure exactly what that is, but I'm pretty sure it won't work as-is.
But, in any case, if you turn on the debug messages, you should get a message telling you what the problem is.
Hey
about that code in the imgurl, it is a variable that is loaded from an SQL database. I tried in this code as well but still gave exact same result so i figured its not that - $imgUrl = "http://fp.profiles.us.playstation.com/playstation/psn/pid/" & {vb:raw userinfo.field5} & ".png";
-
when i remove that extra semi-colon it displays with an error (a screenshot is attached, notice both images are in there. Furthermore, this image is not supposed to be blank but it is supposed to be displayed from the url with the variable from the database)
that was the reason that i added it which led everything to disappear
also in that screenshot the debugger is enabled in the code, no debug message just error in display
Any suggestions?
Thanks
Atli 5,058
Expert 4TB
Ok, I see.
It seems that, for some reason, a part of the PHP code is not being executed, but rather printed to the screen. Can you tell us which type of server you are using, and which versions?
And could you show us the code that is fetching the data from your SQL database? Might help us understand what exactly is going on with that URL. - $imgUrl = "http://fp.profiles.us.playstation.com/playstation/psn/pid/" & {vb:raw userinfo.field5} & ".png";
This is invalid. In PHP we concatenate strings using a single dot, not an ampersand. (I believe the ampersand is used in VB-like languages.)
yes sorry thats my main language used (vb6)
This code is from a template out of like 100 templates. I am trying to embed this in a vBulletin forum V.4.0.3
The template doesnt have a header or a footer as it is a part of a huge PHP system.
Atli 5,058
Expert 4TB
Ahh ok, so this is a part of a vBulletin template? I've never used that before myself, but as I understand it, it has it's own syntax. You can't just put PHP syntax in there.
You need to recreate the PHP code using this vBulletin template syntax, and use that instead. See the vBulletin Manual for details on how to use that syntax. It seems easy enough to use. (Well, it should be, as it is made to be an clean and easy replacement.)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Philipp Lenssen |
last post by:
I created Search.CSS which grabs Google results (using the Web API),
displays as XHTML1.0 Strict + CSS, and recently also adds Thumbnails....
|
by: Qindong Zhang |
last post by:
My asp.net application send image to web browser. The browser begin to display image before download finished. (In Slow Network). How can I force brower to display image after the image downloaded?
...
|
by: Tan |
last post by:
Hi
I am desperate for any help with display image in Gridview
I have a gridview contain surname , forename ..... and image. I could not
display image as my database store the column image as...
|
by: harish |
last post by:
Hi friends
I am facing problem as follow
I can't display image from SQL Database to Picture box
Control.
Here are the codes that I am writing
Dim arrPicture() As Byte = _
|
by: David |
last post by:
I am trying to get an image to appear on all Mondays within the calendar
control. I also want that image to be a link. How can I do this?
|
by: den 2005 |
last post by:
Hi everyone,
Here is code working on..Trying to insert record with a column with Image
or VarBinary datatype in sql database from a existing jpeg image file, then
retrieve this image from...
|
by: Bama |
last post by:
I already stored the image in the SQL database. How do I retrieve it? Any
help is appreciated.
|
by: Ladislav Andel |
last post by:
Hi,
I'm trying to display image through my cgi script in HTML page
via <img src="image.cgi">
Can you give me an example, please?
What should be in the cgi script to display it?
Here is what...
|
by: alexseow |
last post by:
Query.asp
<%@ LANGUAGE="VBSCRIPT" %>
<!-- #include file="../../includes/dbconn.asp"-->
<%
dim MyRs, sqlstr, MyConn
Response.Expires = 0
Response.Buffer = TRUE
Response.Clear
|
by: faizalahmd |
last post by:
How to display image in image control , which is retrieved from the data base . using c# in asp.net
regards,
FAIZAL AHMED.H
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
| |