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.

Best practice for if statement

January 10, 2012 3:25pm

Subscribe [2]
  • #1 / Jan 10, 2012 3:25pm

    Just wondering if there’s a better way to go about this.

    Our site has multiple services. For example, we are a marketing company that does “market research” which is one of our services.
    On that page, I’d like to show the person (professional in this case) that does that service for our company.

    So i figured I’d show that persons info using if statements, depending on which service page they were on, it’d show the professionals info based on their unique entry_id.

    We’ve got about 20 different services and 8 professionals, so this wouldn’t be all the code. I just thought there might be a better way to condense this. Also, would this tax server because of all the requests?

    {exp:channel:entries channel="professionals" dynamic="no" entry_id="10"} 
     {if segment_3 == "web-design-and-development" || segment_3 =="mobile"}
         <a href="http://{title_permalink=marketing/professionals}">{professionals_thumbnail}</a>
    {/if}
    {/exp:channel:entries}
    
    {exp:channel:entries channel="professionals" dynamic="no" entry_id="9"} 
     {if segment_3 == "pay-per-click" || segment_3 =="seo" || segment_3 =="content-marketing"}
         <a href="http://{title_permalink=marketing/professionals}">{professionals_thumbnail}</a>
    {/if}
    {/exp:channel:entries}
    
    {exp:channel:entries channel="professionals" dynamic="no" entry_id="15"} 
     {if segment_3 == "channel-marketing" || segment_3 =="integrated-communications" || segment_3 =="market-research"}
         <a href="http://{title_permalink=marketing/professionals}">{professionals_thumbnail}</a>
    {/if}
    {/exp:channel:entries}

     

  • #2 / Jan 10, 2012 5:00pm

    Rob Allen

    3114 posts

    Complex conditionals like those do add overhead, you’re probably better off with simple conditionals, even if it’s a bit more code:

    {if segment_3 == "web-design-and-development"}
    <a href="http://{title_permalink=marketing/professionals}">{professionals_thumbnail}</a>
    {/if}
    
    {if segment_3 =="mobile"}
    <a href="http://{title_permalink=marketing/professionals}">{professionals_thumbnail}</a>
    {/if}

    That’s not to say never use complex conditionals but for overall performance simple is usually the way to go.

  • #3 / Jan 11, 2012 5:24pm

    Dan Decker

    7338 posts

    Hi skodaminotti,

    I second Rob’s suggestion that simple will always offer better performance. The key thing to focus on is, are you getting the results you expect?

    Since this is not a direct technical issue that you are having, and there is a discussion going on here, I’m going to move this into Community Help.

    Cheers!

  • #4 / Jan 11, 2012 5:26pm

    It works as it should, I just though the code seemed a bit bloated.
    Thank you.

  • #5 / Jan 11, 2012 5:27pm

    It works as it should, I just though the code seemed a bit bloated.
    Thank you.

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

ExpressionEngine News!

#eecms, #events, #releases