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!
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.
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 :(
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;
Thanks a lot guys! ;D
That did the trick. (Btw, there seems to be a ' too much?)
Sorry, yes there is, coding while drunk has its downsides.
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! :)
Querying multiple groups is really not efficient and best avoided if at all possible. How many groups are we talking exactly?
3 groups at max.
The 2 other groups is quite small, only about 10 members..
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.
Thank you! It works perfectly.
Didn't seem to be that slow? Maybe I have a slow host, nevertheless.
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.