By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,237 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,237 IT Pros & Developers. It's quick & easy.

How to not display image if blank

P: 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
Share this Question
Share on Google+
7 Replies


Atli
Expert 5K+
P: 5,058
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

P: 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
Expert 5K+
P: 5,058
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

P: 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, 178 views)
May 23 '10 #5

Atli
Expert 5K+
P: 5,058
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

P: 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
Expert 5K+
P: 5,058
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

Post your reply

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