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.

using member_id in exp:query

February 07, 2013 7:46am

Subscribe [3]
  • #1 / Feb 07, 2013 7:46am

    77120

    15 posts

    Hi,

    Im trying to get the member_forum_rank on a template outside on the forum with

    {exp:query sql="
         SELECT 
         SUM(total_forum_topics+total_forum_posts) 
         AS total_forum_posts_for_rank 
         FROM exp_members 
         WHERE member_id = '<?php global $SESS; echo $SESS->userdata['member_id'];?>' "}
              {exp:query sql="SELECT rank_title FROM exp_forum_ranks WHERE rank_min_posts <= '{total_forum_posts_for_rank}'"}
                   {if count == {total_results}}{rank_title}{/if}
              {/exp:query}
    {/exp:query}

    which work only if i hardcode the member_id, how can i get the {logged_in_member_id} there dynamicly?

    i have php enabled, parse on input and caching is off

    this is the error i get:

    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 ''
    
    Filename: modules/query/mod.query.php
    
    Line Number: 181

    if i only call (leaving the exp:query out)

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

    i get:

    A PHP Error was encountered
    
    Severity: Notice
    
    Message: Trying to get property of non-object
    
    Filename: libraries/Functions.php(680) : eval()'d code
    
    Line Number: 50
  • #2 / Mar 05, 2013 4:14am

    77120

    15 posts

    anyone?

  • #3 / Mar 05, 2013 6:21am

    Bhashkar Yadav

    727 posts

    What is the version of your ExpressionEngine install.

  • #4 / Mar 05, 2013 6:32am

    77120

    15 posts

    v2.5.3 - Build Date: 20120911

  • #5 / Mar 05, 2013 6:40am

    Bhashkar Yadav

    727 posts

    As you using EE2.5.3, so {logged_in_member_id} global variable will work.

    {exp:query sql="
         SELECT 
         SUM(total_forum_topics+total_forum_posts) 
         AS total_forum_posts_for_rank 
         FROM exp_members 
         WHERE member_id = '{logged_in_member_id}' "}
              {exp:query sql="SELECT rank_title FROM exp_forum_ranks WHERE rank_min_posts <= '{total_forum_posts_for_rank}'"}
                   {if count == {total_results}}{rank_title}{/if}
              {/exp:query}
    {/exp:query}

     

  • #6 / Mar 05, 2013 7:33am

    77120

    15 posts

    thanks for your help,

    but {logged_in_member_id} seems to be parsed later then the query :(

  • #7 / Mar 05, 2013 9:44am

    Bhashkar Yadav

    727 posts

    You should have parse parameter with query tag like:

    {exp:query sql="
         SELECT 
         SUM(total_forum_topics+total_forum_posts) 
         AS total_forum_posts_for_rank 
         FROM exp_members 
         WHERE member_id = '{logged_in_member_id}' " parse="inward"}
              {exp:query sql="SELECT rank_title FROM exp_forum_ranks WHERE rank_min_posts <= '{total_forum_posts_for_rank}'"}
                   {if count == {total_results}}{rank_title}{/if}
              {/exp:query}
    {/exp:query}

    Also, can have another solution by using the PHP like:

    $this->EE =& get_instance();
    $member_id = $this->EE->session->userdata['member_id'];
    
    {exp:query sql="
         SELECT 
         SUM(total_forum_topics+total_forum_posts) 
         AS total_forum_posts_for_rank 
         FROM exp_members 
         WHERE member_id = '<?php echo $member_id; ?>' "}
              {exp:query sql="SELECT rank_title FROM exp_forum_ranks WHERE rank_min_posts <= '{total_forum_posts_for_rank}'"}
                   {if count == {total_results}}{rank_title}{/if}
              {/exp:query}
    {/exp:query}

    If you are using above code example, make sure that template is having preference of “PHP Parsing Stage” as “Input”.

  • #8 / Mar 06, 2013 2:45am

    77120

    15 posts

    ah yes thank you,

    the first code doenst work for me.

    but the second one does which is great 😊  thanks

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

ExpressionEngine News!

#eecms, #events, #releases