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.

Query to detect whether a member has made a post and show/hide content if not...

April 13, 2012 10:04am

Subscribe [0]
  • #1 / Apr 13, 2012 10:04am

    oliverhood

    17 posts

    I need to make it so a member of a site can only view the full version of a template if they have made a post in a specific channel. So if they haven’t made a post they see a banner message telling them to do it, etc.

    I have a version of this using the Query module, but it doesn’t seem to work correctly…

    {exp:query sql="select count(*) as my_count from exp_channel_titles where author_id = 'CURRENT_USER' and (channel_id = '2') and (status = 'open') "}
    {if my_count > 1}<div class="alertbox">You haven't created a profile yet. Click <a href="/temps/create-profile/">here</a> to make one.</div>{/if}{/exp:query}

    So I also need that same code to ‘hide’ parts of the profile templates as well, until they have made their own profile.

    I hope that makes sense. Any help is appreciated.

  • #2 / Apr 16, 2012 10:50am

    oliverhood

    17 posts

    I really need a resolution to this soon. Anyone able to help?

  • #3 / Apr 16, 2012 11:12am

    Dan Decker

    7338 posts

    Hi Oliver,

    Thank you for your post and I appreciate you urgency. We do our best to respond within 48hrs during the business week.

    You can accomplish this without the Query module with the channel entries tag:

    {exp:channel:entries channel="my_special_channel" author_id="CURRENT_USER"}
    {if no_results}
     You need to make an entry…
    {if:else}
    You have made an entry
    {/if}
    {/exp:channel:entries}

    Give that a whirl and let me know your results!

    Cheers,

  • #4 / Apr 16, 2012 11:27am

    oliverhood

    17 posts

    Dan,

    That seems to work. Such an obvious solution too, I was expecting that to be a lot harder for some reason.

    I’ll also need to be able to show and hide parts of the profile templates depending on whether the current user has created their own profile, would that use a similar setup using embeds or something?

    EDIT: Is there any reason my Super Admin account wouldn’t see this box, but other normal accounts do?

    EDIT 2: Well, that’s odd… if the {if logged_in} conditional is on the same line as the {exp:channel:entries…} tags, the exp tag doesn’t seem to work… is that a bug?

    So…

    {if logged_in}
    {exp:channel:entries channel="temps" author_id="CURRENT_USER"}
    {if no_results}<div class="alertbox"><strong>You haven't created a profile yet. Click <a href="/temps/create-profile/">here</a> to make one.</strong></div>{if:else}{/if}{/exp:channel:entries}
    {/if}

    works, but…

    {if logged_in}{exp:channel:entries channel="temps" author_id="CURRENT_USER"}
    {if no_results}<div class="alertbox"><strong>You haven't created a profile yet. Click <a href="/temps/create-profile/">here</a> to make one.</strong></div>{if:else}{/if}{/exp:channel:entries}
    {/if}

    Doesn’t seem to?

  • #5 / Apr 18, 2012 2:28pm

    oliverhood

    17 posts

    I still need this resolving:

    I need to be able to show and hide parts of the profile templates depending on whether the current user has created their own profile, would that use a similar setup using embeds or something?

  • #6 / Apr 18, 2012 4:47pm

    Dan Decker

    7338 posts

    Hi Oliver,

    Indeed, place the part you want to show if there hasn’t been a profile created inside the {if no_results}{/if} conditional.

    That would show if the logged in user does not have an entry in the profile channel.

    That can be an embed if you like, but also can be whatever you need it to be.

    Cheers!

  • #7 / Apr 19, 2012 5:57am

    oliverhood

    17 posts

    Indeed, place the part you want to show if there hasn’t been a profile created inside the {if no_results}{/if} conditional.

    I still need some help with this one, to recap…

    I have a template for profiles (in this case ‘Temp’ profiles.)

    This is populated by entries from the ‘temps’ channel.

    The code for this template would need to go inside the ‘check for an entry’ code.

    However, I can’t use the {exp:channel:entries} tags within another pair of those tags and show another user’s profile, so I am a little stuck.

  • #8 / Apr 20, 2012 11:13am

    oliverhood

    17 posts

    I really need to resolve this by the end of the weekend, any pointers? Thanks.

  • #9 / Apr 20, 2012 2:13pm

    Dan Decker

    7338 posts

    Hi Oliver,

    When you need to get {exp:channel:entries} “nested”, you need to embed a template with the nested loop:

    {exp:channel:entries channel="my_special_channel" author_id="CURRENT_USER"}
    {if no_results}
     {embed="template-group/temps"}
    {if:else}
    You have made an entry
    {/if}
    {/exp:channel:entries}

    Then you put your needed *other* channel entries tag in the temps template as usual.

    Cheers,

  • #10 / Apr 23, 2012 7:25am

    oliverhood

    17 posts

    Dan,

    Thanks for that. I think it’s almost there.

    Using the following code:

    {embed="includes/html_begin"}
    {embed="includes/header"}
    <div id="content">
    {if logged_in}
    {exp:channel:entries channel="temps" author_id="CURRENT_USER"}
    {if no_results}
    {embed="/temps/profile-partial"}
    {if:else}
    {embed="/temps/profile-full"}
    {/if}
    {/exp:channel:entries}
    {/if}
    </div>
    {embed="includes/html_end"}

    A profile (my admin account) without a post in the Temps channel functions correctly and shows the ‘partial’ version. A profile in the Temps member group with a profile shows only the header and footer, nothing else. Do I need to pass the entry_id through? Even though it seems to work on the Admin account side?

  • #11 / Apr 24, 2012 6:04pm

    Dan Decker

    7338 posts

    Hi Oliver,

    There is no need to {if:else}, as {if no_results} always wins. Give this a go:

    {embed="includes/html_begin"}
    {embed="includes/header"}
    <div id="content">
    {if logged_in}
     {exp:channel:entries channel="temps" author_id="CURRENT_USER"}
      {if no_results}
       {embed="/temps/profile-partial"}
      {/if}
      {embed="/temps/profile-full"}
     {/exp:channel:entries}
    {/if}
    </div>
    {embed="includes/html_end"}

    Cheers,

  • #12 / Apr 25, 2012 6:41am

    oliverhood

    17 posts

    That seems to work.

    Thanks Dan. Very obvious now I look at it.

  • #13 / Apr 26, 2012 2:11pm

    Dan Decker

    7338 posts

    Hi Oliver,

    My pleasure, glad to see you are all set!

    If you need anything else, just let us know.

    Cheers!

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

ExpressionEngine News!

#eecms, #events, #releases