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.

if conditionals and query module

May 07, 2008 12:17pm

Subscribe [3]
  • #1 / May 07, 2008 12:17pm

    techyogi

    35 posts

    greetings,

    from the post here: http://ellislab.com/forums/viewthread/40382/
    Derek says that vanilla segment if conditionals should be processed before the nested exp:query

    i.e
    {if segment_3 != "this-cool-segment" }
      {exp:query sql="SELECT something FROM table WHERE thing = '{segment_3}’”}
      {something}
      {exp:query}
    {/if}

    But I am not seeing the same results. I am getting an error due to the second query trying to run.
    The error is the table doesn’t exist - which is correct, but only possible if the surrounding conditional is being ignored as i explain below.
    I am on EE 1.6.3
    Here is a simplified template:

    {if "{segment_3}” == “” OR “{segment_3}” == “ALL”}
    {exp:query sql="SELECT COUNT(baby_name_id) AS bntotal FROM baby_names2 WHERE gender = 'M' AND active = 1"}
    {bntotal}
    {/exp:query}
    {if:else}
    {exp:query sql="SELECT COUNT(bn.baby_name_id) AS bntotal FROM baby_names2 bn JOIN baby_rank_{segment_3} AS br ON bn.baby_name_id = br.baby_name_id WHERE bn.active = 1 AND bn.gender = ‘M’”}
    {bntotal}
    {/exp:query}
    {/if}

    the url i am going to is mydomain.com/top_list/Boy
    when a state gets added, it becomes segment_3
    i.e mydomain.com/top_list/Boy/NY

    the second query joins a table whose name is formed from segment_3 - so if there is no segment_3, there is no table for it join, thus throwing the table table does not exist error - which makes perfect sense - the question here is - why is EE trying to execute the second query? - it is as if it is totally ignoring the conditionals.

    standing by..

  • #2 / May 07, 2008 12:19pm

    Robin Sowell

    13255 posts

    Ditch the quotes-
    <pre><code>
    {if segment_3 == "" OR segment_3 == "ALL"}
    [/quote]
    Make any difference?

  • #3 / May 07, 2008 12:21pm

    techyogi

    35 posts

    no difference.

  • #4 / May 07, 2008 12:28pm

    Ingmar

    29245 posts

    Derek says that vanilla segment if conditionals should be processed before the nested exp:query

    Sorry, but you are using advanced conditionals, using {if:else}. So, parse order issue.

  • #5 / May 07, 2008 12:30pm

    techyogi

    35 posts

    hmm, ok.
    then in theory this should work:

    {if segment_3 == "" OR segment_3 == "ALL"}
    {exp:query sql="SELECT COUNT(baby_name_id) AS bntotal FROM baby_names2 WHERE gender = 'M' AND active = 1"}
    {bntotal}
    {/exp:query}
    {/if}

    {if segment_3 != "" AND segment_3 != "ALL"}
    {exp:query sql="SELECT COUNT(bn.baby_name_id) AS bntotal FROM baby_names2 bn JOIN baby_rank_{segment_3} AS br ON bn.baby_name_id = br.baby_name_id WHERE bn.active = 1 AND bn.gender = ‘M’”}
    {bntotal}
    {/exp:query}
    {/if}

    but it doesn’t.
    same error.

  • #6 / May 07, 2008 12:36pm

    Ingmar

    29245 posts

    Try using different names, and not {bntotal} twice.

  • #7 / May 07, 2008 12:41pm

    techyogi

    35 posts

    like this:

    {if segment_3 == "" OR segment_3 == "ALL"}
    {exp:query sql="SELECT COUNT(baby_name_id) AS bntotal FROM baby_names2 WHERE gender = 'M' AND active = 1"}
    {bntotal}
    {/exp:query}
    {/if}

    {if segment_3 != "" AND segment_3 != "ALL"}
    {exp:query sql="SELECT COUNT(bn.baby_name_id) AS bntotal2 FROM baby_names2 bn JOIN baby_rank_{segment_3} AS br ON bn.baby_name_id = br.baby_name_id WHERE bn.active = 1 AND bn.gender = ‘M’”}
    {bntotal2}
    {/exp:query}
    {/if}

    still no go…

  • #8 / May 07, 2008 1:27pm

    techyogi

    35 posts

    funny thing is, this works as intended:

    {if segment_3 == "" OR segment_3 == "ALL"}
    NO SEG3 OR SEG3=ALL
    {!--
    {exp:query sql="SELECT COUNT(baby_name_id) AS bntotal FROM baby_names2 WHERE gender = 'M' AND active = 1"}
    {bntotal}
    {/exp:query}
    --}
    {/if}
    
    {if segment_3 != "" AND segment_3 != "ALL"}
    {segment_3}
    {!--
    {exp:query sql="SELECT COUNT(bn.baby_name_id) AS ZZZ FROM baby_names2 bn JOIN baby_rank_{segment_3} AS br ON bn.baby_name_id = br.baby_name_id WHERE bn.active = 1 AND bn.gender = 'M'"}
    {ZZZ}
    {/exp:query}
    --}
    {/if}

    and if make the template just:

    {if "1" == "2"}
    {exp:query sql="SELECT COUNT(bn.baby_name_id) AS ZZZ FROM baby_names2 bn JOIN baby_rank_{segment_3} AS br ON bn.baby_name_id = br.baby_name_id WHERE bn.active = 1 AND bn.gender = 'M'"}
    {ZZZ}
    {/exp:query}
    {/if}

    it STILL throws the query error - pointing back to the if conditional being totally ignored.

    granted if i throw the sql in another template and replace the exp:query with embeds, it works as intended, but then i have to maintain two sets of output code which i am trying to avoid at all costs.

  • #9 / May 07, 2008 1:53pm

    techyogi

    35 posts

    well for time constraints i gave in and moved my queries to separate templates and used embeds inside the if conditionals.
    as always, thanks for the quick replies !
    cheers.

  • #10 / May 07, 2008 11:49pm

    Sue Crocker

    26054 posts

    techyogi, so is it OK to close the thread?

  • #11 / May 08, 2008 11:43am

    techyogi

    35 posts

    sure. thanks as always guys!

  • #12 / May 08, 2008 11:56am

    Ingmar

    29245 posts

    Very well, then.

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

ExpressionEngine News!

#eecms, #events, #releases