list users who belongs to role X
PLEASE NOTE! The following snippet is user submitted. Use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.
Drupal doesn't have a built-in function to show all the users of a particular user role. Here's a pair of snippets that show a list of users, similar to that on the user administration page (and mostly reuse the code from function user_admin_account() in user.module). For a list of user profiles, see http://drupal.org/node/144735.
These work in both Drupal 4.7 and 5.1.
This first snippet gives you output that looks just like what you see at the administer >> users screen:
<?php
//choose the role to list by role ID value.
// Note ID 1 == anonymous, ID 2 == authenticated user
// so valid values here are > 2.
$rid = 3;
?>
<h2>A list of all users with Role ID <?php print $rid?></h2>
<br/>
<?php
$header = array(
array('data' => t('Username'), 'field' => 'u.name'),
array('data' => t('Status'), 'field' => 'u.status'),
array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
array('data' => t('Last access'), 'field' => 'u.access'),
t('Operations')
);
$sql = "SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u INNER JOIN {users_roles} ur ON u.uid=ur.uid WHERE ur.rid = $rid";
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
$status = array(t('blocked'), t('active'));
while ($account = db_fetch_object($result)) {
$rows[] = array(theme('username', $account),
$status[$account->status],
format_interval(time() - $account->created),
$account->access ? t('%time ago', array('%time' => format_interval(time() - $account->access))) : t('never'),
l(t('edit'), "user/$account->uid/edit", array()));
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 50, 0);
print ($output);
?>Below is a second variant that is more useful to display to users without permission to administer users. It removes the "edit" column, and sorts the list by the username:
<?php
//choose the role to list by value.
// Note ID 1 = anonymous, ID 2 = authenticated user
// so valid values here are > 2.
$rid = 3;
?>
<h2>A list of all users with Role ID <?php print $rid?></h2>
<br/>
<?php
$header = array(
array('data' => t('Username'), 'field' => 'u.name', 'sort' => 'asc'),
array('data' => t('Status'), 'field' => 'u.status'),
array('data' => t('Member for'), 'field' => 'u.created'),
array('data' => t('Last access'), 'field' => 'u.access')
);
$sql = "SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u INNER JOIN {users_roles} ur ON u.uid=ur.uid WHERE ur.rid = $rid";
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
$status = array(t('blocked'), t('active'));
while ($account = db_fetch_object($result)) {
$rows[] = array(theme('username', $account),
$status[$account->status],
format_interval(time() - $account->created),
$account->access ? t('%time ago', array('%time' => format_interval(time() - $account->access))) : t('never'));
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 50, 0);
print ($output);
?>