Forum Moderators: coopster

Message Too Old, No Replies

if statement for file type

displaying a certain image based on what the url file type is

         

jspeed

9:06 pm on May 12, 2009 (gmt 0)

10+ Year Member



Hello, I am somewhat new to PHP. What I am trying to do is display a certain image based on what file type is in an URL.

If a link is <a href="blabla.pdf">Bla Bla</a>, then directly after it automatically posting the img code for a PDF icon image. (or excel icon, or word icon etc.)

Any direction in how to achieve this would be much appreciated.

Thank you

penders

11:03 pm on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A bit of an aside... IMHO, rather than appending an IMG element to the source, I would add a CLASS attribute to the anchor and use CSS to include the icon as a background image of the anchor. A separate class for each file type. I don't think the icon should be part of the content in this case.

Are you creating (ie. writing out) your links with PHP when you create your page? If not, then I would consider using JavaScript (link to forum) [webmasterworld.com] instead of PHP to solve this problem. I would have thought that using JS you could more easily step through the DOM and add the appropriate CLASS attributes to the anchors. JS would be OK in this case since the icon is really only a presentational 'nice to have' and does not necessarily add to the page content, so it would not matter if the JS did not run on a device that did not have JS enabled. It could also be a useful function to add to your JS library that you could call on any page that required file-type icons on links.

Sorry for the narrative, I just don't think that PHP is the right tool for the job in this case. Unless you are writing the links themselves with PHP...?

jspeed

7:17 pm on May 13, 2009 (gmt 0)

10+ Year Member



Thanks for the reply. I am not writing the links in PHP, I was just unaware of other ways to achieve this. I am even more in the dark as how to achieve this using JS though. Guess I will post something in the JS forum. Thanks.