473,396 Members | 1,764 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,396 software developers and data experts.

How to not display image if blank

4
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

Expand|Select|Wrap|Line Numbers
  1. <a href="http://us.playstation.com/publictrophy/index.htm?onlinename={vb:raw userinfo.field5}">
  2. <img src="http://fp.profiles.us.playstation.com/playstation/psn/pid/{vb:raw userinfo.field5}.png">
  3. </a><br  /><br  />
  4.  
Thanks
May 23 '10 #1
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // The URL to the image you want to
  3. // use in your code.
  4. $imgUrl = "http://fp.profiles.us.playstation.com/playstation/psn/pid/wrongurlexample.png";
  5.  
  6. // Fetch the response headers the server
  7. // sends when the image is requested.
  8. $headers = get_headers($imgUrl);
  9.  
  10. // See if the first line includes 200,
  11. // which is the HTTP response code for success.
  12. if(stristr($headers[0], "200")) 
  13. {
  14.     // The server can successfully serve
  15.     // the image, so we can use it!
  16.     echo "<img src=\"{$imgUrl}\" alt=\"My Image\">";
  17. }
  18. else {
  19.     // The server can not serve the image.
  20.     // Do whatever you want to do instead,
  21.     // like serve an error image.
  22.     echo '<img src="error.png" alt="An error occured">';
  23. }
  24. ?>
May 23 '10 #2
StJust
4
@Atli
Hey,

Thank you for your response. This is the code I implemented for my page

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // The URL to the image you want to
  3. // use in your code.
  4. $imgUrl = "http://fp.profiles.us.playstation.com/playstation/psn/pid/{vb:raw userinfo.field5}.png";
  5.  
  6. // Fetch the response headers the server
  7. // sends when the image is requested.
  8. $headers = get_headers($imgUrl);
  9.  
  10. // See if the first line includes 200,
  11. // which is the HTTP response code for success.
  12. if(stristr($headers[0], "200"))
  13. {
  14.    // The server can successfully serve
  15.    // the image, so we can use it!
  16.    echo '<a href="http://us.playstation.com/publictrophy/index.htm?onlinename={vb:raw userinfo.field5}"><img src=\"{$imgUrl}\"></a>';
  17. };
  18. else {
  19.    // The server can not serve the image.
  20.    // Do whatever you want to do instead,
  21.    // like serve an error image.
  22.    echo '<a href="http://us.playstation.com/publictrophy/index.htm?onlinename={vb:raw userinfo.field5}"><img src="cpstyles/darkvision/misc/nopid.png"></a>';
  23. }
  24. ?>
  25.  
Now everything disappeared. Theres absolutely nothing displaying.

What seems to be the problem and how do i fix it?

Thanks
May 23 '10 #3
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.
May 23 '10 #4
StJust
4
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

Expand|Select|Wrap|Line Numbers
  1. $imgUrl = "http://fp.profiles.us.playstation.com/playstation/psn/pid/" & {vb:raw userinfo.field5} & ".png";
  2.  
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
Attached Images
File Type: jpg error.jpg (18.1 KB, 191 views)
May 23 '10 #5
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.

Expand|Select|Wrap|Line Numbers
  1. $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.)
May 23 '10 #6
StJust
4
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.
May 23 '10 #7
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.)
May 24 '10 #8

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

Similar topics

7
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....
0
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? ...
1
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...
3
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 = _
2
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?
3
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...
3
by: Bama | last post by:
I already stored the image in the SQL database. How do I retrieve it? Any help is appreciated.
3
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...
7
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
5
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
0
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...
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: 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
marktang
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,...
0
Oralloy
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,...
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...

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.