Easy CSS/php default image

In the process of developing an e commerce shopping cart, I had the need to make sure an image was displayed at all times… even if the link is broken for some reason. This easy solution will make sure that your large detail view image is loaded last… thus making your site appear that it loads faster (as per standards-compliant rules, while at the same time ensuring no [X] boxes pop up:

<?
$image = "./path/to/images/" . $itemnumber . ".jpg"; //generate path from already gathered info
if (!(file_exists($image))) // make sure file exists
$image = "./path/to/images/notfound.jpg"; // if file doesn't exist, revert to default
?>
<div id="detailView" style = "background-image: URL('<? echo $image; ?> ');"> //update style information real-time

Hopefully this should help somebody out who is trying to do something similar or develop their own e commerce solution. Saves on javascript code, minimized end-user page-load time, and uses minimal server resources.

This entry was posted in css, development, php and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WP Hashcash

Subscribe without commenting