1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Message Count

Discussion in 'XenForo Development Discussions' started by LPH, Jul 25, 2013.

  1. LPH

    LPH Well-Known Member

    My brain is on hold. It will return in 2 weeks. In the meantime ...

    This code returns the message count for the visitor logged into the site.

    global $XF; echo 'messages:' $XF->visitor->get('message_count') . '<br />';
    Instead, I would like the message count for the user who posted the message.
    Last edited: Jul 25, 2013
  2. Jeremy

    Jeremy XenForo Moderator Staff Member


    That should work (untested tho).
  3. LPH

    LPH Well-Known Member

    OK. Thank you. Let me try and I'll let you know :)

    Update: It barfed.
  4. Jeremy

    Jeremy XenForo Moderator Staff Member

    If it doesn't work, the function is probably wrong, but its in XenForo_Model_User.
  5. LPH

    LPH Well-Known Member

    The message_count is in several functions within the model but I'm not sure of the form to take to return the message count.


    Last edited: Jul 25, 2013
  6. Jeremy

    Jeremy XenForo Moderator Staff Member

    What I posted above should work. The query for XenForo_Model_User::getUserById($user) will return the row for the specific user. It returns an array of the data, so you would access the message count. The following would access my message count if it was run here:

    $user $userModel->getUserById('81');
    $user['message_count']; //3821
  7. LPH

    LPH Well-Known Member

    Clearly I'm not understanding the logic here. Sorry.

    I'm trying to recreate the postbit shown in XF on an external page. The avatar shows and below it shows the text phrase "message:" ... but the message count is returning mine all the time and not the one for the person who did the posting.

    The template is easy: $user.message_count

    You've hard coded 81. How can I get it to be the user that posted the message?

    Sorry. And thanks for hanging in there.

                $userModel XenForo_Model::create('XenForo_Model_User');
    $id $user_id;
    $user $userModel->getUserById($id);
    Is blank.
  8. LPH

    LPH Well-Known Member


    Time to walk away from this computer.

    Why can't I just call a template from an external file?
    Why can't I just figure out how to get a message_count?
    Why ...
  9. Jeremy

    Jeremy XenForo Moderator Staff Member

    I hard coded 81 as an example. You'll need to code it correctly based on (I'm assuming) the author of the WP post to correctly fetch their information.

    As long as you have XF set up to where you can Autoload or access the files, you could very well compile templates and use them in your outside systems.
  10. LPH

    LPH Well-Known Member


    Took a short stretch. I'm back and in the Xen ... ;)

    Took your code, copied it directly. Changed user to 1. Worked to always show user 1 message count.

    Now it needs to be dynamic.

    Tried this code:

                $userModel XenForo_Model::create('XenForo_Model_User');
    $user $userModel->getUserById($input['user_id']);
    $user['message_count']; //3821
    This show a blank count. So, $input['user_id'] failed ...

    Sidebar: I'm going to ask about loading templates externally after I figure out message count on an external file ...
  11. Jeremy

    Jeremy XenForo Moderator Staff Member

    To the main code: Your $input['user_id'] is not set and/or isn't a valid XenForo user.

    As to the external templates, that's quite a bit more complicated, I'll only be able to nudge you in the correct direction.
  12. LPH

    LPH Well-Known Member

    I'm giving up. It's such a silly thing and should have taken a few seconds of typing.

    I appreciate your help but this is absurd. A message count should be easy to get.

    Hangs head in shame.

                global $XF;
    ' <span class="xf_messages">Messages: ' ;
    $id $comment->user_id;  // Needs to dynamically pull the user_id
    $user XenForo_Model::create('XenForo_Model_User')->getUserById($id);
    // echo $user['message_count'] . '<br />Username' . $username;
    echo '</span><br />';
    Went to the WordPress database to pull the user_id. This works but have no idea why $user_id failed to pull from XenForo.

    Last edited: Jul 25, 2013

Share This Page