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.

Having problems with a conditional in related entries

October 23, 2011 1:52pm

Subscribe [4]
  • #1 / Oct 23, 2011 1:52pm

    Mike Mella

    178 posts

    So I’m building a job board site and I have two channels: Jobs and Applications.  The Applications channel contains a relationship field that connects to the jobs channel.  The idea is, when a job-seeker is viewing a job at the URL http://www.site.com/job/10, he can click an Apply button that would take him to a Safecracker form that lets him post an entry to the Applications channel with job #10 pre-populated as the related entry, since that’s the job he was viewing when he clicked the Apply button.

    If the user has already applied for that job, instead of the Apply button there would be a line that reads “You have already applied for this”.

    Here’s the code I’m trying:

    {exp:channel:entries channel="jobs" require_entry="yes"}
    
    {!-- If there is an entry in the Applications channel whose author is the same as the current user, tell him he applied.  If not, show the Apply button: --}       
    {reverse_related_entries channel="applications"}
     {if logged_in_member_id == '{author_id}'}
      You applied for this job.
     {if:else}
      <a href="http://{path=job/apply/{segment_2}}">Apply for this job</a>
     {/if}
    {/reverse_related_entries}
    
    {/exp:channel:entries}

    This apparently doesn’t work.  I suspect this is because the conditional is actually saying “for all entries where the logged in member is the author, tell him he’s applied.  For all other entries, show the Apply button.”  So as long as anyone has applied for that job, the else statement returns true and the Apply button displays.  It seems like what I need is a way to restrict the results of the reverse_related_entries to only those posted by the logged in member.

    So what I need is some conditional that says “if the current user has not posted an entry to the Applications channel related to this job, then show the Apply button.”

    Thoughts?

  • #2 / Oct 23, 2011 2:51pm

    Wouter Vervloet

    758 posts

    Sadly the reverse_related_entries tag doesn’t support an author parameter, so that’s off the table. You might have some luck using a custom SQL query.

    {exp:query sql="SELECT COUNT(*) as application_count FROM exp_channel_titles ct LEFT JOIN exp_relationships r ON r.rel_parent_id = ct.entry_id WHERE ct.author_id={logged_in_member_id} AND r.rel_child_id={entry_id}"}
      {if application_count > 0}
        You have already applied for this job.
      {if:else}
        <button>Apply</button>
      {/if}
    {/exp:query}

    That should do the trick. If you’re not comfortable with using SQL like this in your templates, you could always use Playa to handle the relationships… playa has a lot more flexibility when it comes to outputting stuff on the frontend.

    – Wouter

  • #3 / Oct 23, 2011 3:17pm

    James Smith

    259 posts

    Hi Mike,

    Wouter’s suggestion is cleaner and more efficient, but if you want to avoid the SQL, you could probably also do it with an embedded template like this:

    {exp:member:custom_profile_data}
     {exp:channel:entries author_id="{member_id}" channel="applications"}
      {related_entries id="job"}   
       {if entry_id == segment_2}
        {!-- this user has already applied for this job --}
       {if:else}
        {!-- this user has not applied for this job (but has applied for some others) --}
       {/if}
      {/related_entries}
      {if no_results}
       {!-- this user has made no applications for ANY jobs --}
      {/if}
     {/exp:channel:entries}
    {/exp:channel:custom_profile_data}
  • #4 / Oct 24, 2011 5:24am

    ahmad saad

    364 posts

    I am sorry Wouter Vervloet the reverse_related_entries tag doesn’t support an author parameter, but he use {author_id} var and it supported.

    your problem simply is parssing order. the Standard Global Variables parssing last after the conditional and {author_id} var.

    so if the {author_id} var= 1 for exampel your conditional parssing like this:

    {if logged_in_member_id == '1'} without parss logged_in_member_id Global Variables value .

    so whatever the author_id value the conditional return false.

    for work around this problem I advise pass the logged_in_member_id Global Variables value with embed var(ex.. log_id) and use it in the conditional .

    and put (’‘)around embed var so your conditional become

    {exp:channel:entries channel="jobs" require_entry="yes"}
    
    {!-- If there is an entry in the Applications channel whose author is the same as the current user, tell him he applied.  If not, show the Apply button: --}       
    {reverse_related_entries channel="applications"}
     {if '{embed:log_id}'== '{author_id}'}
      You applied for this job.
     {if:else}
      <a href="http://{path=job/apply/{segment_2}}">Apply for this job</a>
     {/if}
    {/reverse_related_entries}
    
    {/exp:channel:entries}

    I think this work for u

  • #5 / Oct 24, 2011 5:47am

    Wouter Vervloet

    758 posts

    Hi Ahmad,

    I think you should re-read Mike’s message, as he isn’t having trouble with the conditional… his is evaluating that conditional for every application related to that job. So basically if there are 5 applications he would get this (if one of the applications was made by the current user):

    <a href="/job/apply/job_url_title">Apply for this job</a>
    You applied for this job.
    <a href="/job/apply/job_url_title">Apply for this job</a>
    <a href="/job/apply/job_url_title">Apply for this job</a>
    <a href="/job/apply/job_url_title">Apply for this job</a>

    I also wasn’t talking about the author_id variable, but about the author-parameter (with which you can filter by author_id on the channel:entries tag).

    If you are having trouble with accessing the current logged in member_id, I have an extensions that adds all member variable as early accessible global variables.

    Mike, let us know if any of the solutions work for you.

    — Wouter

  • #6 / Oct 24, 2011 6:24am

    ahmad saad

    364 posts

    I am sorry Wouter i didn’t mean bother you .

    he would get exactly what u said.

    and his code must give him what he wont if he add ({''}) around logged_in_member_id.

    but the problem as I said is parssing order.

    and it’s a very common problem and I think support tem must notice this in the future ver so we can use Standard Global Variables in conditional or in {exp: parameters as u suggested.

    and even he use your code he will get the same parssing problem becuse {exp: parssing befor Standard Global Variables and he get the following error:

    Error Number: 1064
    
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '} AND r.rel_child_id=1' at line 1
    
    SELECT COUNT(*) as application_count FROM exp_channel_titles ct LEFT JOIN exp_relationships r ON r.rel_parent_id = ct.entry_id WHERE ct.author_id={logged_in_member_id} AND r.rel_child_id=1
    
    Filename: modules/query/mod.query.php
    
    Line Number: 181

    and please give me extensions that adds all member variable if it’s var parssing earlier than conditional and {exp: it’s well be very useful

  • #7 / Oct 24, 2011 6:44am

    James Smith

    259 posts

    You can work around some of those member variable parse order issues by wrapping channel:entries tag with custom_profile_data tag - further discussion of that here: http://ellislab.com/forums/viewthread/169997/

  • #8 / Oct 24, 2011 7:39am

    Wouter Vervloet

    758 posts

    You should be able to get around the parsing order issue by adding parse=‘inward’ to the {exp:query} tag.

  • #9 / Oct 24, 2011 8:06am

    ahmad saad

    364 posts

    thank’s At the Gates I think that’s it please Mike try it and let us know if the solutions work for you

    your code will be like this:

    {exp:member:custom_profile_data}
    {exp:channel:entries channel="jobs" require_entry="yes"}
    
    {!-- If there is an entry in the Applications channel whose author is the same as the current user, tell him he applied.  If not, show the Apply button: --}       
    {reverse_related_entries channel="applications"}
     {if '{member_id}' == '{author_id}'}
      You applied for this job.
     {if:else}
      <a href="http://{path=job/apply/{segment_2}}">Apply for this job</a>
     {/if}
    {/reverse_related_entries}
    
    {/exp:channel:entries}  
    {/exp:member:custom_profile_data}

    and the parse=‘inward’ not work for {exp:query} tag and the error still appeers

  • #10 / Oct 24, 2011 8:43am

    James Smith

    259 posts

    Ahmad, as Wouter pointed out, that code will still output ‘Apply for this job’ for all other applications that have been made on that job - reverse_related_entries will loop through every entry, not just the one that the logged-in member submitted.

  • #11 / Oct 24, 2011 11:07am

    Mike Mella

    178 posts

    Thanks for the help guys. 

    I’m trying Wouter’s query, but I’m just getting a blank page when I visit that template with an entry ID appended to the URL.  Do I need to modify anything in your code?

    Incidentally, am I missing something, or is there no documentation for the {exp:member:custom_profile_data} tag anywhere?

  • #12 / Oct 24, 2011 11:26am

    James Smith

    259 posts

    Odd, it indeed doesn’t seem to be in the 2.x docs. I’ve just taken a direct look in the 2.2.2 member module file though, (modules/member/mod.member.php) and it’s still there around line 2625 so it should work all the same -

    It’s still documented in the EE1 docs if you need it: http://expressionengine.com/legacy_docs/modules/member/custom_profile_data.html

    Sorry the majority of my work is still in EE1.

  • #13 / Oct 24, 2011 11:45am

    Mark Bowen

    12637 posts

    Hi Mike,

    I’m going to move this post on down to the Community Help forums as I believe it will be a better fit there.

    Since there isn’t anything actually technically wrong with ExpressionEngine then this is more of a how-to post than anything.

    Thanks,

    Mark

  • #14 / Oct 24, 2011 12:07pm

    Mike Mella

    178 posts

    Hi Mike,

    I’m going to move this post on down to the Community Help forums as I believe it will be a better fit there.

    Sure thing Mark. I wasn’t sure what the How-to forum was called now.

  • #15 / Oct 24, 2011 12:47pm

    Wouter Vervloet

    758 posts

    Hi Mike,

    What does your template look like when try my exp:query solution? (it does need to be nested in the {exp:channel:entries} tag).

    — Wouter

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

ExpressionEngine News!

#eecms, #events, #releases