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} statement within {exp:query} tag. Is this possible?

March 06, 2010 10:21pm

Subscribe [3]
  • #1 / Mar 06, 2010 10:21pm

    Patrick Mc

    19 posts

    {exp:query limit="10" sql="SELECT vehicles.make, vehicles.model FROM vehicles WHERE vehicles.make = '{segment_3}' {if '{segment_4}'}AND vehicles.model = '{segment_4}'{/if}"}
    
       {make} - {model}<br>
         
    {/exp:query}

    This is just a very basic example but {if} statements are just not working for me in {exp:query} tags. Is this even possible?

    Thanks for any help.

  • #2 / Mar 07, 2010 3:42pm

    Greg Salt

    3988 posts

    Hi Patrick,

    You should be able to do this by explicitly declaring the exp:query tags between the conditional like this:

    {if segment_4 != ''}
    {exp:query limit="10" sql="SELECT vehicles.make, vehicles.model FROM vehicles WHERE vehicles.make = '{segment_3}' AND vehicles.model = '{segment_4}'"}
       {make} - {model}<br>
    {/exp:query}
    {/if}
    {if segment_4 == ''}
    {exp:query limit="10" sql="SELECT vehicles.make, vehicles.model FROM vehicles WHERE vehicles.make = '{segment_3}'"}
       {make} - {model}<br>
    {/exp:query}
    {/if}

    However, please refer to our Knowledge Base article on Troubleshooting Conditionals, especially section 6.

    Cheers

    Greg

  • #3 / Mar 07, 2010 8:41pm

    Patrick Mc

    19 posts

    Right on! Thank you for the help! 😊

  • #4 / Mar 08, 2010 1:00am

    John Henry Donovan

    12339 posts

    Glad Greg was able to help. Feel free to start a new thread if you have any more questions.

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

ExpressionEngine News!

#eecms, #events, #releases