ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Using if statement for channel field

September 05, 2012 12:05pm

Subscribe [2]
  • #1 / Sep 05, 2012 12:05pm

    shotgunflat

    116 posts

    Hi,

    I have a client who will be uploading files to their site.  I’m going to be using icons next to the document to display type (ie Word, Excel or PDF).  I wanted to know what the best what to do that would be.  Can I use a channel field type in an if statement? 

    I’m hoping to do something like this, where file_type is my select dropdown name.

    {if 'file_type' == "PDF"}/images/ui/icon_pdf.png
    {if:else} {if 'file_type' == "Word"}/images/ui/icon_word.png
    {if 'file_type' == "Excel"}/images/ui/icon_excel.png
    {/if}

    Thanks,
    Drew

  • #2 / Sep 05, 2012 12:12pm

    shotgunflat

    116 posts

    Got it!  Just had to correct my syntax.

    {if '{file_type}' == "PDF"}/images/ui/icon_pdf.png
    {if:elseif '{file_type}' == "Word"}/images/ui/icon_word.png
    {if:elseif '{file_type}' == "Excel"}/images/ui/icon_excel.png
    {/if}
  • #3 / Sep 05, 2012 3:53pm

    John St-Amand

    865 posts

    One quick suggestion - you could simplify even further to not use a conditional at all if you take advantage of what the file system module offers (granted, you’d need to rename your icons to do this):

    {your_file_field}
    /images/ui/icon_{extension}.png
    {/your_file_field}

    And so that way, the icon is dynamically triggered by the uploaded file’s extension.  It does mean though that you have to predict which file extensions will be uploaded and create an icon for each - though it seems you’re likely doing that anyway to some extent.  And in this fashion, each icon would have to be a single format (in this example, png).  Then it’s independent of what the user selects in a “file type” field and is instead tied directly to what they have uploaded.  What that does mean is you might have to have two icons for Word docs - one for .doc and one for .docx - but otherwise, pretty simple and requires no conditional overhead.

    Hope you find that useful.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases