News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Count the users inside a spesific membergroup

Started by ara_is_sweet, May 05, 2012, 08:04:16 PM

Previous topic - Next topic

ara_is_sweet

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!

NanoSector

#1
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.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

ara_is_sweet

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 :(

Arantor

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;
Holder of controversial views, all of which my own.


ara_is_sweet

Thanks a lot guys!    ;D

That did the trick. (Btw, there seems to be a ' too much?)

Arantor

Sorry, yes there is, coding while drunk has its downsides.
Holder of controversial views, all of which my own.


ara_is_sweet

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! :)

Arantor

Querying multiple groups is really not efficient and best avoided if at all possible. How many groups are we talking exactly?
Holder of controversial views, all of which my own.


ara_is_sweet

3 groups at max.
The 2 other groups is quite small, only about 10 members..

Arantor

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.
Holder of controversial views, all of which my own.


ara_is_sweet

Thank you! It works perfectly.
Didn't seem to be that slow? Maybe I have a slow host, nevertheless.

Arantor

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.
Holder of controversial views, all of which my own.


Advertisement: