View Full Version : list all users of a usergroup
Hornstar
12-30-2010, 10:48 PM
How would I list all users from a usergroup?
I think you just need to use "Search For Users" (under Users in the ACP), and choose the group you want from the drop-down menu.
Hornstar
12-30-2010, 11:24 PM
I was a bit vauge sorry. I meant how would I list all users from a usergroup on a template/page.
eg. I have created a file called users.php and a template called users.
I want to list all users from usergroup x, y z like this on the page users.php:
Usergroup x: user 1, user 2, user 3
usergroup y: user 1, user 2, user 3
usergroup z: user 1, user 2, user 3
My first thinking was there may be a variable?
Is there any variable which can list all users from a usergroup?
My second thinking was I need to query the users in the usergroup, and then display it. (I need help on the code for this part if so)
Thanks in advance.
OK, sorry - I guess that's why you posted in the Programming Discussion area.
Anyway, I think you want something like this:
$groupid = 1; // set this to the group id you want
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid");
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
That does one user group, so you can put that whole thing in a loop, or you could modify the query to get all groups at once and handle the separate lists in the while loop.
Also this only handles primary user groups and not membership in secondary groups. (And also I haven't tried this code at all).
Hornstar
12-31-2010, 12:38 AM
Yeah that does look like something I would need. However I can't get it to function just yet.
Maybe I am doing something wrong. Any chance you could test it out (also output 2 usergroups).
Thanks again.
Edit: Got it to work with 1 usergroup now (So big thanks for that)
$groupid = 5; // set this to the group id you want
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid");
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
echo "Super Mods: $str";
However not sure which is the best way to get multiple usergroups listed. You mentioned "That does one user group, so you can put that whole thing in a loop, or you could modify the query to get all groups at once and handle the separate lists in the while loop."
Could you give me an example of that.
Also is there a way to get it to list people in both primary and secondary?
Thanks again.
I could do that. But I also remembered, what you want pretty much already exists in showgroups.php (like this: https://vborg.vbsupport.ru/showgroups.php)
Edit: posted this before I saw your edit above.
--------------- Added 1293763857 at 1293763857 ---------------
You mentioned "[/B][/COLOR]That does one user group, so you can put that whole thing in a loop, or you could modify the query to get all groups at once and handle the separate lists in the while loop."
Could you give me an example of that.
Thanks again.
An example of which way, the second one?
The first way is easier off the top of my head:
$groups = array("Super Mods" => 5, "Admins" => 6, "Mods" => 7); // set this to the group id you want
foreach ($groups as $groupname => $groupid)
{
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid");
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
echo "$groupname: $str <BR>";
}
but again I haven't tried it so hoepfully there aren't any syntax errors.
Hornstar
12-31-2010, 12:56 AM
This is what I am trying to achieve on the page:
Usergroup 4: user1, user2, user 3 etc.
Usergroup 9: user2, user 6, user 8 etc.
So which method you feel will do the above best will be fine. If possible can you also get it to work with both primary and secondary usergorups.
Thanks again.
So which method you feel will do the above best will be fine.
See above...
Also is there a way to get it to list people in both primary and secondary?
That might take a little while, I don't know how to do that offhand.
Hornstar
12-31-2010, 01:00 AM
I missed your reply before, trying that now. one sec. Will edit this when I am done. Thanks.
Edit: yeah that worked great
$groups = array("Super Mods" => 5, "Admins" => 6, "Mods" => 7); // set this to the group id you want
foreach ($groups as $groupname => $groupid)
{
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid");
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
echo "$groupname: $str <BR></BR>";
}
The only improvement would be to have it work with both primary and secondary.
Gotta say tho, you have been really great :)
If you work out how to get it to work with primary and secondary you will be legendary.
Thanks again.
OK, it turned out to be pretty easy (thanks to the code in showgroups.php):
$groups = array("Super Mods" => 5, "Admins" => 6, "Mods" => 7);
foreach ($groups as $groupname => $groupid)
{
$users = $vbulletin->db->query_read("SELECT username FROM ".TABLE_PREFIX."user
WHERE usergroupid = $groupid OR FIND_IN_SET($groupid, membergroupids)");
$str = '';
$sep = '';
while ($user = $vbulletin->db->fetch_array($users))
{
$str .= $sep . $user['username'];
$sep = ', ';
}
// $str is comma separated list of users in $groupid
$vbulletin->db->free_result($users);
echo "$groupname: $str <BR></BR>";
}
I was thinking of trying to do it the other way so that it would be only one query, but really it's starting to get to the point where it would have been easier to start with showgroups.php. And as long as you're not putting this on your home page or anything, it probably won't make any difference.
If you work out how to get it to work with primary and secondary you will be legendary.
Well, not so much legendary, more like 'too much free time', but anyway...
Hornstar
12-31-2010, 02:07 AM
Thanks!
I'd stick with legendary...I spent hours and hours trying to do this on my own. I even looked through the showgroups and it was just all past me.
Now that I have this, I can easily copy and paste the list of names from a specific usergorup and use it to tag all users from a usergroup (Dbtech tag mod) or send them all a pm, etc.
So thanks again.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.