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.

How can I use {member_id} inside of an {exp:query sql="..."} query?

June 05, 2008 2:53pm

Subscribe [2]
  • #1 / Jun 05, 2008 2:53pm

    OutofControl

    164 posts

    EDITED by author: I accidently put member_group in the example. It is now member_id.

    I am trying to create an un-ordered list based on an SQL query:

    {exp:query sql="SELECT CONCAT(m_field_id_10,'|',m_field_id_11,'|',m_field_id_12) as chapters FROM exp_member_data as d WHERE  d.member_id='{member_id}'"}
    {exp:weblog:entries weblog="crew_chapter" entry_id="{chapters}"}
    <li>{title}</li>
    {/exp:weblog:entries}
    {/exp:query}


    The above sql query works perfectly from the MySQL CLI if I substitute {member_id} with my actual member ID which is 1442, with these results:

    +----------+
    | chapters |
    +----------+
    | 16|17|18 | 
    +----------+

    The next step I took was to modify the SQL query so that instead of {member_id} I hard-coded my actual member id, and bingo, it worked! Except I need the output based on the currently logged in user, not always on me 😊

    I know that {member_id} alone as a variable works fine, as I tested by putting just that tag in my template, and it output my member id 1442.

    So, how can I get the current member_id into the sql query above?

  • #2 / Jun 05, 2008 2:59pm

    Ingmar

    29245 posts

    I think it’s a parse order issue. Are you averse to PHP? If not, you can try using

    <?php global $SESS; echo $SESS->userdata['member_id'];?>

    PHP parsed on input. (BTW, you actually have {member_group} in your example. A typo?)

  • #3 / Jun 05, 2008 4:11pm

    OutofControl

    164 posts

    Yup that was a typo! Thanks.

    No aversion to PHP at all, and to boot, your solution solved my issue perfectly. Thank very much!

  • #4 / Jun 05, 2008 4:13pm

    Ingmar

    29245 posts

    Glad to hear it 😊

  • #5 / Jun 05, 2008 4:58pm

    OutofControl

    164 posts

    Hmm. Maybe not completely fixed yet. This is sort of a different question, but still related to the original.

    When I load the page with no segment 3 in the url, it shows all the data I expect to see. But if I add the third segment to the url in the browser, my list displayed gets limited to only the results that are related to that specific entry defined by the third segment.

    To clarify:

    This works fine:
    http://mydomain.com/groups/mygrouplistpage

    This displays only results from the above SQL query in group_1
    http://mydomain.com/groups/mygrouplistpage/group_1

    Any way to force {exp:weblog:entries} to ignore segments. Which, yes, is completely counterintuitive to how EE works.

  • #6 / Jun 05, 2008 5:04pm

    Ingmar

    29245 posts

    Any way to force {exp:weblog:entries} to ignore segments. Which, yes, is completely counterintuitive to how EE works.

    You can still do it, of course, by adding dynamic=“off” to your tag.

  • #7 / Jun 05, 2008 5:07pm

    OutofControl

    164 posts

    Wow, it is obviously way past bedtime for me. I did know that but couldn’t see it through the thicket of code I created. Thank you for stating the obvious.

  • #8 / Jun 05, 2008 5:12pm

    Ingmar

    29245 posts

    Ha, you won’t believe how often I’ve been bitten by that one. Sometimes it takes more than one pair of eyes…

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

ExpressionEngine News!

#eecms, #events, #releases