Counting members in a usergroup question

#1
We imported our vBulletin forum into Xenforo and at first left all the usergroups untouched.
We had a "premium usergroup" which became after importing the "Primary" usergroup in xF just as the were in vB.
To calculate how many users were in that usergroup (#12) I used this query which gave me the exact count of the members in this usergoup:

<?php

class michael_example_index
{
public static function getHtml()
{
// get database
$db = XenForo_Application::get('db');

// assign userId
$usergroup = '12';

// get username from user_id
$premium = $db->fetchOne("
SELECT COUNT(*)
FROM xf_user
WHERE user_group_id = ?
", $usergroup);

// echo result
echo $premium;
}
}

?>


We tried to clean up things a bit and followed the suggestions from xF to make everybody a "registered user" as the Primary and any other usergroup they might belong to as the Secondary usergroup. No problems.
Therefore everybody form the "premium usergroup" has now "Registered" as the primary and "Premium" as the secondary.
This Premium usergroup is still #12

Problem is the query now returns zero

I guess it has to do with the fact they belong to 2 usergroups now?
If so any help how to change the query?

Thanks
 

Brogan

XenForo moderator
Staff member
#2
Why don't you use the built in search feature in the ACP and just set the primary and secondary user group criteria as required?
 
#3
Why don't you use the built in search feature in the ACP and just set the primary and secondary user group criteria as required?
I should have been more clear on this. Sorry about that.
I know I can use the ACP to see the users in this group and that works fine.
I created a template to show some stats in my sidebar and used this query to show the counts. It worked fine when those usergroups were only in the "primary" usergroup but since they are in "registered" and "premium" it stopped working

Screen Shot 2016-11-19 at 8.18.21 PM.png
 

James

Well-known member
#4
Your query is querying the user_group_id column but I believe secondary ones are stored in a separate column are they not?

You have already initialised the xenforo application you should use the built in "isMemberOf" function. You can find the helper in xenforo/template/helper/core.php which should point you to the original function.
 
Top