Users Query under a group

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
irlwebdesign
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Wed Jun 01, 2011 12:40 pm

Users Query under a group

Post by irlwebdesign » Mon Feb 12, 2018 11:46 am

Hi lads,

I am trying to pull down users data under Authors group but I am not able to do it after expending a couple of hours.

How do I complete the following query to get users under a specific group?.

Code: Select all

$db =& JFactory::getDBO();
$query = "SELECT * FROM #__users" ;
$db->setQuery($query);
$users = $db->loadObjectList();
Thanks in advance. :)

User avatar
toivo
Joomla! Exemplar
Joomla! Exemplar
Posts: 9609
Joined: Thu Feb 15, 2007 5:48 am
Location: Oxford, UK
Contact:

Re: Users Query under a group

Post by toivo » Fri Feb 16, 2018 10:00 pm

Here is one way to list users from a nominated group:

Code: Select all

$cr			= PHP_EOL;
$usergroup	= 'Author';
$db     	= JFactory::getDbo();
$query 		= $db->getQuery(true);
$query
	->select($db->quoteName('id'))
	->from($db->quoteName('#__usergroups'))
	->where($db->quoteName('title') . ' = \'' . $usergroup . '\'');
$db->setQuery($query);
if (!$group_id = $db->loadResult())
{
	echo 'usergroup ' . $usergroup . ' not found' . $cr;
	exit;
}
echo $usergroup . ' group_id = ' . $group_id . $cr;
$query 		= $db->getQuery(true);
$query
	->select('*')
	->from($db->quoteName('#__users', 'u'))
	->join('INNER', $db->quoteName('#__user_usergroup_map', 'm') . ' ON (' . $db->quoteName('u.id') . ' = ' . $db->quoteName('m.user_id') . ')')
	->where($db->quoteName('group_id') . ' = ' . $group_id);
$db->setQuery($query);
$users	= $db->loadObjectList();
if (!$count	= count($users))
{
	echo 'usergroup ' . $usergroup . ' has no users' . $cr;
}
echo $usergroup . ' usergroup has ' . $count . ' users' . $cr;
foreach($users AS $user)
{
	echo $user->name . ' ' . $user->email . $cr;
}
Toivo Talikka, Global Moderator
my first programs were assembled and run in 16KB :)
troubleshooting smtp and other articles https://talikka.com/joomla

User avatar
toivo
Joomla! Exemplar
Joomla! Exemplar
Posts: 9609
Joined: Thu Feb 15, 2007 5:48 am
Location: Oxford, UK
Contact:

Re: Users Query under a group

Post by toivo » Sat Feb 17, 2018 7:45 am

This version uses the method getUsersByGroup() which simplifies the query.

Code: Select all

$cr			= PHP_EOL;
$usergroup	= 'Author';
$db     	= JFactory::getDbo();

$query = $db->getQuery(true)
	->select('id')
	->from($db->quoteName('#__usergroups'))
	->where($db->quoteName('title') . ' = \'' . $usergroup . '\'');
$db->setQuery($query);
if (!$group_id = $db->loadResult())
{
	echo $usergroup . ' not found' . $cr;
	exit;
}

$access		= new JAccess;
// include child groups: ($usergroup, true)
if (!$id_list = $access->getUsersByGroup($group_id))
{
	echo $usergroup . ' has no users' . $cr;
	exit;
}
$query = $db->getQuery(true)
	->select('*')
	->from('#__users')
	->where('id IN (' . implode(',', $id_list) . ')');
$db->setQuery($query);
$users	= $db->loadObjectList();

echo $usergroup . ' has ' . count($users) . ' users' . $cr;
foreach($users AS $user)
{
	echo $user->name . ' ' . $user->email . $cr;
}
Toivo Talikka, Global Moderator
my first programs were assembled and run in 16KB :)
troubleshooting smtp and other articles https://talikka.com/joomla

irlwebdesign
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Wed Jun 01, 2011 12:40 pm

Re: Users Query under a group

Post by irlwebdesign » Mon Feb 19, 2018 8:44 am

Thanks a million :)


Post Reply

Return to “Joomla! 3.x Coding”