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.

Conditional parse order (Query module)

October 08, 2009 6:40am

Subscribe [2]
  • #1 / Oct 08, 2009 6:40am

    Wil Linssen

    49 posts

    I have a sub template with an embedded variable. I want to run a query if that variable is empty however the query is run regardless. Remove the query and the statement works fine…

    {if "" == "{embed:currentid}" || !embed:currentid }
    
     no id
     
    {if:else}
     
     {exp:query sql="SELECT .... WHERE `id`={embed:currentid}"}
    
     {/exp:query}
    
    {/if}

    Any help??

  • #2 / Oct 08, 2009 6:56am

    Ingmar

    29245 posts

    Your syntax is off. Why not simply use

    {if embed:currentid != ""}
       {exp:query sql="..."}
    
       {/exp:query}
    {/if}
  • #3 / Oct 08, 2009 7:00am

    Wil Linssen

    49 posts

    Sorry, should have mentioned that’s how I originally had it, that snippet above is after exhausting all variants of the conditional.

  • #4 / Oct 08, 2009 9:43am

    ender

    1644 posts

    template conditionals don’t prevent things inside them from running, they only decide which parts of the template are output to the browser.  You’ll need to use PHP conditionals and queries if you need to conditionally run a query.

  • #5 / Oct 08, 2009 10:18am

    Wil Linssen

    49 posts

    Thanks ender, I’ll do that.

  • #6 / Oct 13, 2009 12:21pm

    Wil Linssen

    49 posts

    The PHP conditionals wouldn’t work because of embedded variables, so to solve this one I just put the second part of the statement in an embed. That works a treat: might help if people are having the same problem in the future.

    Incidentally it will also solve {if no_results} in the query: you can just run another, different query to find alternative results.

    {exp:query sql="SELECT `something` FROM `somewhere` WHERE 1"}
    
      {something}
      
      {if no_results}
        
        {embed="template_group/.alternative_query" currentid="{embed:currentid}"}
      
      {/if}
      
    {/exp:query}
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases