Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: StarredSkies on June 18, 2021, 08:08:12 PM

Title: Making a simple group member counter?
Post by: StarredSkies on June 18, 2021, 08:08:12 PM
Hello! So I want to make a simple group member counter. For example, the layout would be like this w/o any special CSS/HTML.

Group Name #1 | 5 ♀ | 4 ♂ | 2 ⚧
Group Name #2 | 1 ♀ | 1 ♂ | 3 ⚧
Group Name #2 | 2 ♀ | 4 ♂ | 7 ⚧

Basically, I want to display the group's name, along with the number of members in each group according to their gender assigned on-site (regular + additions w/ more genders modification). Would there be any possible way to do this?
Title: Re: Making a simple group member counter?
Post by: lather on June 18, 2021, 09:14:36 PM
Yes, you can display members selected by group and sex  with the admin member search tool so it can be done. Just need to add code to count totals.
Title: Re: Making a simple group member counter?
Post by: StarredSkies on June 18, 2021, 10:00:53 PM
Yes, I figured that would be the case, but I want this displayed as a box on my index, not within the member search tool. Unfortunately, I am not very versatile in code and am unsure on how to code such a thing w/o knowing where to start.
Title: Re: Making a simple group member counter?
Post by: Sir Osis of Liver on June 18, 2021, 10:18:42 PM
It's not simple, you'd have to run a query on members table to sort members by id_group and gender.
Title: Re: Making a simple group member counter?
Post by: StarredSkies on June 19, 2021, 06:56:05 PM
Hypothetically couldn't you take the gender count of the entire website from the stats page, then take the membercount of each group to potentially find the answer? Once again, I'm not well versed in PHP/JavaScript. Any help would be appreciated.
Title: Re: Making a simple group member counter?
Post by: Pipke on June 21, 2021, 03:43:27 PM
maybe something like this:(for smf2.0.*)
create this function in Load.php

function usergroupgender()
{
global $smcFunc, $context;

$result = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.gender, mem.id_group, mg.group_name
FROM {db_prefix}members as mem
LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)
WHERE gender > {int:gender_var}
',
array(
'gender_var' => 0,
)
);

while ($row = $smcFunc['db_fetch_assoc']($result))
{
//male
if ($row['gender'] == 1) {
$context['usergroupgender'][$row['group_name']]['count_m'][] = $row['id_member'];
$context['usergroupgender'][$row['group_name']]['Male'] = count($context['usergroupgender'][$row['group_name']]['count_m']);
}
//female
if ($row['gender'] == 2) {
$context['usergroupgender'][$row['group_name']]['count_f'][] = $row['id_member'];
$context['usergroupgender'][$row['group_name']]['Female'] = count($context['usergroupgender'][$row['group_name']]['count_f']);
}
}

$smcFunc['db_free_result']($result);
}


then you can call the function in any template and use the variables with the global var $context['usergroupgender']
Code (template example) Select

usergroupgender();
echo'<pre>';
print_r($context['usergroupgender']);
echo'</pre>';


Code (example array output) Select

[Administrator] => Array
        (
            [count_m] => Array
                (
                    [0] => 1
                )

            [Male] => 1
        )

Title: Re: Making a simple group member counter?
Post by: live627 on June 22, 2021, 05:45:53 AM
The stats page seems to calculate that on the fly, so its data cannot be used here.