Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: ara_is_sweet - toukokuu 05, 2012, 08:04:16 IP

Otsikko: Count the users inside a spesific membergroup
Kirjoitti: ara_is_sweet - toukokuu 05, 2012, 08:04:16 IP
I have searched and searched for this, but I couldn't find any info regarding this other then it's a heavy query(?)  :(
There dosent seem like there is any function that does this, even though its done in the admin area.

I need a function in my SSI that can paste the number of members of visible membergroups.

I.e: Admin group : 2

If anyone can help me out, it would be awsome! PS: im a php nub!
Otsikko: Re: Count the users inside a spesific membergroup
Kirjoitti: NanoSector - toukokuu 05, 2012, 08:25:18 IP
Hi ara_is_sweet!

Heavy query? Where?

If you know the ID of the membergroup:

$result = $smcFunc['db_query']('', '
SELECT id_member
FROM {db_prefix}members
WHERE id_group = {int:group}',
array(
'group' => 'the ID of the group'
));

$num_users = $smcFunc['db_num_rows']($result);


That's all.

EDIT: Forgot the $smcFunc skin for the db_num_rows.
Otsikko: Re: Count the users inside a spesific membergroup
Kirjoitti: ara_is_sweet - toukokuu 05, 2012, 09:07:03 IP
Hey, and thanks for the fast reply. Yes, I know the ID, but how do I call the function?

<?php 
echo'' $result['num_users'] , '';
?>

Something like this? Sorry, but I'm new to php :(
Otsikko: Re: Count the users inside a spesific membergroup
Kirjoitti: Arantor - toukokuu 05, 2012, 09:28:32 IP
That won't work if you have any users who have that group as a secondary group, or if it's a post-count based group.

What you actually need to do is (and this is also faster than actually getting all the names, too):

$result = $smcFunc['db_query']('', '
SELECT COUNT(id_member)
FROM {db_prefix}members
WHERE id_group = {int:group} OR id_post_group = {int:group} OR FIND_IN_SET({int:group}, additional_groups)'',
array(
'group' => 'the ID of the group'
));

list($num_users) = $smcFunc['db_fetch_row']($result);


To do something with the number afterwards, simply:
echo $num_users;
Otsikko: Re: Count the users inside a spesific membergroup
Kirjoitti: ara_is_sweet - toukokuu 05, 2012, 09:57:52 IP
Thanks a lot guys!    ;D

That did the trick. (Btw, there seems to be a ' too much?)
Otsikko: Re: Count the users inside a spesific membergroup
Kirjoitti: Arantor - toukokuu 05, 2012, 10:00:59 IP
Sorry, yes there is, coding while drunk has its downsides.
Otsikko: Re: Count the users inside a spesific membergroup
Kirjoitti: ara_is_sweet - lokakuu 04, 2013, 11:46:18 AP
Necroing my own thread here..

I've been using the script for a long time, but how do I query multiple groups?
Hope there is someone out there! :)
Otsikko: Re: Count the users inside a spesific membergroup
Kirjoitti: Arantor - lokakuu 04, 2013, 11:48:25 AP
Querying multiple groups is really not efficient and best avoided if at all possible. How many groups are we talking exactly?
Otsikko: Re: Count the users inside a spesific membergroup
Kirjoitti: ara_is_sweet - lokakuu 04, 2013, 12:01:23 IP
3 groups at max.
The 2 other groups is quite small, only about 10 members..
Otsikko: Re: Count the users inside a spesific membergroup
Kirjoitti: Arantor - lokakuu 04, 2013, 12:07:29 IP
Hmm, OK.

$result = $smcFunc['db_query']('', '
SELECT COUNT(id_member)
FROM {db_prefix}members
WHERE id_group IN ({int:group1}, {int:group2}, {int:group3}) OR id_post_group IN ({int:group1}, {int:group2}, {int:group3}) OR FIND_IN_SET({int:group1}, additional_groups) OR FIND_IN_SET({int:group2}, additional_groups) OR FIND_IN_SET({int:group3}, additional_groups)',
array(
    'group1' => 'the ID of the first group',
    'group2' => 'the ID of the second group',
    'group3' => 'the ID of the third group',
));

list($num_users) = $smcFunc['db_fetch_row']($result);


This will not be fast.
Otsikko: Re: Count the users inside a spesific membergroup
Kirjoitti: ara_is_sweet - lokakuu 04, 2013, 12:17:03 IP
Thank you! It works perfectly.
Didn't seem to be that slow? Maybe I have a slow host, nevertheless.
Otsikko: Re: Count the users inside a spesific membergroup
Kirjoitti: Arantor - lokakuu 04, 2013, 12:19:55 IP
Slow is a relative term. The fact it's already hitting every row manually on the members table is a killer. Doing it several times over is not a significantly worse killer - but for any serious amount of members (more than a few thousand) it's going to grind.