Pa**************@gmail.com wrote:
So I have some 'groups' which 'users' can join. There is no
enrollment limit on these 'groups'. How should I store the list of
users enrolled in the group?
I'd like to be able to quickly determine the groups a user is in, and
the users in a group. Seems like a common problem but I can't come up
with an effecient solution.
Depends on whether the groups are a dynamic lot or a static lot. If the
number of groups is limited to a smallish number, say 32, won't change
and you don't mind the group names being hard-coded you can use this.
You can add new groups later without creating conflicts - to a limit.
Define a bunch of constants where each constant is a bit in a 32 bit
unsigned integer.
For example:
define( '_GROUP_DEBUGGER', 0x00000001 );
define( '_GROUP_THIEF', 0x00000002 );
define( '_GROUP_FIGHTER', 0x00000004 );
define( '_GROUP_MONK', 0x00000008 );
define( '_GROUP_MAGE', 0x00000010 );
define( '_GROUP_PHP', 0x00000020 );
define( '_GROUP_SQL', 0x00000040 );
....
define( '_GROUP_LIMIT', 0x80000000 );
However you choose to store the group mask, you will be able to test for
group membership with a bitwise AND and the named constant:
if( $user['group'] & _GROUP_PHP ) {
// do stuff for group member
} else {
// not a group member
}
Users can be in multiple groups and assigning group membership is easy:
$user['group'] = _GROUP_DEBUGGER | _GROUP_PHP | _GROUP_LIMIT;
To search for particular group members, just test for all users whose
group mask & _GROUP_XXXXX is not 0.
CJW