News:

Want to get involved in developing SMF? Why not lend a hand on our GitHub!

Main Menu

Making a simple group member counter?

Started by StarredSkies, June 18, 2021, 08:08:12 PM

Previous topic - Next topic

StarredSkies

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?

lather

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.

StarredSkies

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.

Sir Osis of Liver

It's not simple, you'd have to run a query on members table to sort members by id_group and gender.
When in Emor, do as the Snamors.
                              - D. Lister

StarredSkies

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.

Pipke

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
        )

"If something is wrong, fix it if you can. But train yourself not to worry: Worry never fixes anything."

Click here to view my mods for SMF

Hey 👋 Did i helped... you like what i do. You can now buy me a coffee! ☕

live627

The stats page seems to calculate that on the fly, so its data cannot be used here.

Advertisement: