Skip to main content
Inspiring
May 24, 2008
Question

click link to display image - what if image doesn't exist ?

  • May 24, 2008
  • 1 reply
  • 399 views
We have a database of over 4,000 rows of sales products, each with a unique product code eg. C189
we keep photos in a folder beneath the coldfusion pages, using the unique product code as the filename eg. C189.jpg

Typically a user can display 50 rows of products. Each of those rows has a link which the user can click to display the corresponding photo. This part works fine - I just set the image filename as the [product_code].jpg

The issue I have is, we don't have photos for all the products, so sometimes a user will click a link and no image will display. What I'd like is a way of detecting if the image is there when a user clicks a link.

I understand there's a CF command called FileExists , but the reason I haven't chosen to use that is I'm guessing it would be slow - when the user displays say 50 rows of products, wouldn't <CFIF fileExists> have to check each row's image ?

I was hoping there may be a better way ?
    This topic has been closed for replies.

    1 reply

    Inspiring
    May 24, 2008
    well, 'better' is a subjective thing...
    here are some options that i myself have used in the past:

    1) write and run a query to find all products in your db that do not
    have a photo. then add the missing photos, mark the products as 'no
    photo' in your db, or do whatever else you deem appropriate

    2) you can use fileexists() on the page that displays the image, and if
    it doe not, sow a "sorry, image not available..." message (and maybe log
    it somewhere to later have a list of products without photos)

    3) IMG tag has a seldom used and not widely known attribute: onError.
    you can set this attribute to a little js code to change the src of the
    image to a generic 'not available' or some other image, which will then
    be displayed instead of the missing photo:
    <img src="products/#product_code#.jpg"
    onerror="this.src='products/notavailable.jpg'">

    [credit for the onError method goes, iirc, to ben nadel -
    www.bennadel.com - that's where i picked it up, i believe]

    hope you find one of these options useful.

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    Inspiring
    May 24, 2008
    options 2 and 3 are "do-able"

    I'll give 2 a try and see what performance hit it gets.

    And I like option 3 - will defintiely give that a go

    thanks for your help