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.

Conditionals across multiple channels

October 24, 2011 7:52pm

Subscribe [2]
  • #1 / Oct 24, 2011 7:52pm

    dehuszar

    99 posts

    I am trying to run an and/or conditional check across 2 channels and am having trouble figuring out how to phrase the code.

    What I have thus far is a simple:

    {if logged_in}               
                   {exp:channel:entries channel="applications" author_id="CURRENT_USER" limit="1" dynamic="no" orderby="edit_date" sort="desc"}
                      {if no_results}You must complete an <a href="http://{site_url}application/" title="Fill out an Application">Application</a> to be eligible for job-matching.{/if}
                   {/exp:channel:entries}
                   {exp:channel:entries channel="resumes" author_id="CURRENT_USER" limit="1" dynamic="no" orderby="edit_date" sort="desc"}
                      {if no_results}You must complete a <a href="http://{site_url}resume/" title="Fill out a Resume">Resume</a> to be eligible for job-matching.{/if}
                   {/exp:channel:entries}
                {/if}

    What I want to do is have code that effectively says ‘if CURRENT_USER has no posts in channel x, and if CURRENT_USER has no posts in channel y, then display STRING’  I know I can run conditionals on multiple channels using the |; i.e. channel=“applications|resumes”, but I’m not sure how I would do an ‘or’ conditional; i.e. ‘if CURRENT_USER has no posts in channel x, OR if CURRENT_USER has no posts in channel y, then display STRING’

    Using {exp:channel:entries} seems a little verbose to assemble sentences the way one does with normal {if} statements.

    Is there a simple way to create these kinds of statements?  I’m starting to think that SQL tags and enabling PHP in the templates are the only way at this point, but that will likely end up being fairly verbose as well, and I’m still a little puzzled on where to start. 

    Any ideas?

    Thanks in advance,
    Sam

  • #2 / Oct 24, 2011 9:55pm

    John St-Amand

    865 posts

    Hmm. The trouble I see is that the message displayed from a no results finding is different, so combining them into a single conditional would be very difficult without getting into a query and/or PHP I suspect.  Based on what you are saying though, it seems like an OR statement would throw the wrong results, since one of the conditions could easily be met while the other is not and vice versa, which would result in a message either way.  But the structure you have so far seems to consider both very differently. So a combined conditional might not produce the result you’re thinking of.

    Or is it that you’re looking to add a condition AFTER the two you have in there now for some other message?  As it stands right now, i think you have the conditionals built the right way - what are you looking to have happen differently?

  • #3 / Oct 24, 2011 10:45pm

    dehuszar

    99 posts

    I’m trying to dynamically create a sentence that reads, “You need to fillout an application and a resume.”  Where “application” or “resume” are returned if the user does not have an entry authored in the relevant channel(s) and the ‘and’ between them prints if the author hasn’t created an entry in either channel.

  • #4 / Oct 24, 2011 11:10pm

    John St-Amand

    865 posts

    Ah.  So “You need to fillout an application and a resume” would apply if they don’t have an entry in EITHER channel.  But then if one of the two of them have an entry but the other doesn’t, you need separate in-context lookups? So it could be something like:

    {if logged_in}          
                   {exp:channel:entries channel="applications|resumes" author_id="CURRENT_USER" limit="20" dynamic="no" orderby="edit_date|edit_date" sort="desc|desc"}
                      {if no_results}You need to fillout an application and a resume.{/if}
    
              {embed="embeds/applications-lookup"}
              {embed="embeds/resumes-lookup"}
    
                   {/exp:channel:entries}
                   
    {/if}

    And then in the embedded templates:

    {exp:channel:entries channel="applications" author_id="CURRENT_USER" limit="1" dynamic="no" orderby="edit_date" sort="desc"}
                      {if no_results}You must complete an <a href="http://{site_url}application/" title="Fill out an Application">Application</a> to be eligible for job-matching.{/if}
    
                   {/exp:channel:entries}
    {exp:channel:entries channel="resumes" author_id="CURRENT_USER" limit="1" dynamic="no" orderby="edit_date" sort="desc"}
                      {if no_results}You must complete a <a href="http://{site_url}resume/" title="Fill out a Resume">Resume</a> to be eligible for job-matching.{/if}
    
                   {/exp:channel:entries}


    Really, since in this case you’re using “CURRENT_USER” and nothing else for the lookup, you could use snippets rather than embeds because at present you don’t actually need to pass a variable through to the embed.

    Please let me know if this helps.

  • #5 / Oct 26, 2011 4:06pm

    dehuszar

    99 posts

    Holy cow, John!  That was excellent advice, thank you!

    For anyone else trying this, here’s what I did in the end.

    {if logged_in}               
       {exp:channel:entries channel="applications|resumes" author_id="CURRENT_USER" limit="1" dynamic="no" orderby="edit_date" sort="desc"}
          {if no_results}
             <strong>You must complete an <a href="http://{site_url}application/" title="Fill out an Application">Application</a> and a <a href="http://{site_url}resume/" title="Fill out an Resume">Resume</a> to be eligible for job-matching.</strong>           
          {/if}
          {embed=includes/has_app}
          {embed=includes/has_resume}
       {/exp:channel:entries}
       {exp:channel:entries channel="applications" author_id="CURRENT_USER" limit="1" dynamic="no" orderby="edit_date" sort="desc" status="Open"}
          {if total_results > 0}
             {embed=includes/has_both}
          {/if}
       {/exp:channel:entries}
    {/if}

    My includes then looked like so:
    (embed=includes/has_app)

    {exp:channel:entries channel="applications" author_id="CURRENT_USER" limit="1" dynamic="no" orderby="edit_date" sort="desc" status="Open"}
       {if no_results}<strong>You must complete an <a href="http://{site_url}application/" title="Fill out an Application">Application</a> to be eligible for job-matching.</strong>{/if}
    {/exp:channel:entries}

    (embed=includes/has_resume)

    {exp:channel:entries channel="resumes" author_id="CURRENT_USER" limit="1" dynamic="no" orderby="edit_date" sort="desc"}
       {if no_results}<strong>You must complete a <a href="http://{site_url}resume/" title="Fill out a Resume">Resume</a> to be eligible for job-matching.</strong>{/if}
    {/exp:channel:entries}

    (embed=includes/has_both)

    {exp:channel:entries channel="resumes" author_id="CURRENT_USER" limit="1" dynamic="no" orderby="edit_date" sort="desc" status="Open"}
          {if total_results > 0}To express your interest in this position, email <a href="mailto:[email protected]">[NAME REDACTED]</a>{/if}
    {/exp:channel:entries}

    It turns out this particular approach didn’t work with Snippets.  The nested{/exp:channel:entries} tags would close out the container {exp:channel:entries}. 

    As nesting {exp:channel:entries} tag pairs is sorta considered a no-no (or so I’ve read around the forums) I’m going to try my hand at some in-template PHP.  Getting a true / false boolean assigned to variables for each lookup will probably run faster, be less complicated to evaluate AND/OR/NEITHER conditionals and be less prone to future gotchas, but what I’ve got in place now works!

    Thanks again, John!

  • #6 / Oct 29, 2011 9:25am

    John St-Amand

    865 posts

    No worries.  Glad I could help you out.

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

ExpressionEngine News!

#eecms, #events, #releases