MyBB Community Forums

Full Version: Check usergroup from array/comma separated list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I have a comma separated list of group ID's, user inputted (in the ACP), but then how can I get those into my PHP IF statement?

I can use explode() and put all the ID's into an array, but then how do I use that and have it check the groups?

I know HOW to check, just not the groups from the list/array. I would check it with:

if($mybb->user['usergroup'] != "4" || $mybb->user['usergroup'] != "8")
put your acp list into an array, then append the users additional groups to the usergroup and then do an array intersect. the result is an array then they are in the group

edit: once you append the additional groups and the main usergroup, then push that into an array, then do the intersect
How do I do that? Just say I got the list into the array, now what? Can you give me the code?
This function should do the trick.

function check_ugroups($groups_check)
{
	global $mybb;
	
	if($groups_check == '')
	{
		return false;
	}
	
	$groups = explode(',', $groups_check);
	$add_groups = explode(',', $mybb->user['additionalgroups']);
	
	if(!in_array($mybb->user['usergroup'], $groups))
	{
		if($add_groups)
		{
			if(count(array_intersect($add_groups, $groups)) == 0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			return false;
		}
	}
	else
	{
		return true;
	}
}

You'd use it like this:

$exempts = 4,5; // separate groups with a comma
if(check_ugroups($exempts))
{
	// if user belongs to the specified group id, do this...
}
Thanks Spencer Big Grin