Message Count

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.

PHP:
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:
XenForo_Model_User::getUserById($id)['message_count']

That should work (untested tho).
 
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.

prepareUserOrderOptions

prepareUserConditions
 
Last edited:
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:
PHP:
<?php

$userModel = XenForo_Model::create('XenForo_Model_User');
$user = $userModel->getUserById('81');
print $user['message_count']; //3821
 
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.

PHP:
            $userModel = XenForo_Model::create('XenForo_Model_User');
             
             $id = $user_id;
             
             $user = $userModel->getUserById($id);
             
             echo $user['message_count'];

Is blank.
 
man-punching-computer.webp

Time to walk away from this computer.

[rant]
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 ...
[/rant]
 
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.
 
OK.

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:

PHP:
            $userModel = XenForo_Model::create('XenForo_Model_User');
             $user = $userModel->getUserById($input['user_id']);
             echo $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 ...
 
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.
 
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.


PHP:
            global $XF;
             
             echo ' <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'];
             
             // 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:
Top Bottom