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.

Help with embedding exp:channel:entries inside a query

December 16, 2012 5:53pm

Subscribe [2]
  • #1 / Dec 16, 2012 5:53pm

    ovation01

    42 posts

    I’m trying to use exp:query and it is not allowing me to use the {exp:channel:entries} tag within it.

    The goal of my code:
    - Pull a custom member field from the URL ({segment_3})
    - Use {exp:query} to look at that custom field in the URL and find the user’s group_id
    - If the group_id matches, then display the contents of {exp:channel:entries}

    What’s working:
    - I can get the query to recognize the custom field, and to grant permission based on group_id, and display anything NOT within the {exp:channel:entries}

    What’s broken:
    - Once we’ve validated group_id, I cannot get it to display the contents of {exp:channel:entries}

    Interesting Note:  I am using a custom field in the query ({segment_3}). If I changed this to a built-in field, like username, then the {exp:channel:entries} data displays just fine.

    The broken code:

    {exp:query sql="
    SELECT m_field_id_3, group_id, a.member_id
    FROM exp_member_data a
    JOIN exp_members b 
    ON a.member_id=b.member_id
    WHERE m_field_id_3='{segment_3}'
    "}
    
    {if group_id=='8'}
    
    {exp:channel:entries channel='audio' category='219|220|259' rdf='off' limit='44' start_on='2012-09-01 12:00 am' show_future_entries='yes' show_expired='no' } 
    {title}<br>
    {/exp:channel:entries}
    
    <item>
    <title>{exp:xml_encode}TEST HEADLINE{/exp:xml_encode}</title>
    <description>{exp:xml_encode}THIS IS A TEST ITEM THAT IS NOT CONTAINED INSIDE EXP:CHANNEL:ENTRIES {/exp:xml_encode}</description>
     </item>
    
    {/if} 
    
    {/exp:query}

    Any idea?

  • #2 / Dec 16, 2012 10:04pm

    Gareth Davies

    491 posts

    Are you sure you need the query. {member_group} for example is a global variable and you can get any custom member fields with the custom profile data tag (http://ellislab.com/expressionengine/user-guide/modules/member/index.html#custom-profile-data-tag)

    Failing that, your code above isn’t working due to the order in which ExpressionEngine processes the code. You’d need to put the code in an embedded template as an embedded template is processed last so for example:

    {exp:query sql="
    SELECT m_field_id_3, group_id, a.member_id
    FROM exp_member_data a
    JOIN exp_members b 
    ON a.member_id=b.member_id
    WHERE m_field_id_3='{segment_3}'
    "}
    
    {embed="site/mytemplate" group_id="{group_id}"}
    
    {/exp:query}

    and your embedded template

    {if '{embed:group_id}'=='8'}
    
    {exp:channel:entries channel='audio' category='219|220|259' rdf='off' limit='44' start_on='2012-09-01 12:00 am' show_future_entries='yes' show_expired='no' } 
    {title}<br>
    {/exp:channel:entries}
    
    <item>
    <title>{exp:xml_encode}TEST HEADLINE{/exp:xml_encode}</title>
    <description>{exp:xml_encode}THIS IS A TEST ITEM THAT IS NOT CONTAINED INSIDE EXP:CHANNEL:ENTRIES {/exp:xml_encode}</description>
     </item>
    
    {/if}

    Finally if you don’t mind a bit of PHP in your template you can use the session class to get the group id (http://ellislab.com/expressionengine/user-guide/development/usage/session.html)

    $group_id = $this->EE->session->userdata['group_id'];
  • #3 / Dec 17, 2012 8:04am

    ovation01

    42 posts

    Unfortunately I think I have to use the query. For this particular use, my user will not actually be logging into the website, so there will be no session attached to him. Instead the info must be displayed based on a custom field contained in {segment_3} of the URL.

    I will try the embedding.

    However, here’s an interesting note. I have previously used this same code (below) to render the {exp:channel:entries} tag without any embedding necessary. 

    The only difference is that instead of pulling a custom field from the URL, I was pulling the native “username” field from the URL.  Yet everything rendered fine without embedding.

    Any clue as to why a custom field would make things parse differently than a native field?

    Example:

    {exp:query sql="SELECT group_id, member_id, join_date, username FROM exp_members WHERE username='{segment_3}' "}
    
    
    {if group_id=='8'}
    
    {exp:channel:entries channel="audio" category="219|220|259" rdf="off" limit="44" start_on="2012-09-01 12:00 am" show_future_entries="yes" show_expired="no" } 
    {title}<br>
    {/exp:channel:entries}
    
    <item>
    <title>{exp:xml_encode}TEST HEADLINE{/exp:xml_encode}</title>
    <description>{exp:xml_encode}THIS IS A TEST ITEM THAT IS NOT CONTAINED INSIDE EXP:CHANNEL:ENTRIES {/exp:xml_encode}</description>
     </item>
    
    
    {/if} 
    
    {/exp:query}

     

  • #4 / Dec 17, 2012 1:08pm

    Gareth Davies

    491 posts

    Sorry, didn’t read your post properly before replying and not sure why it would behave differently for a custom field.

    In this case however I would recommend embedding to resolve your issue.

    Also, it might be worth asking over at http://expressionengine.stackexchange.com/

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

ExpressionEngine News!

#eecms, #events, #releases