List Users From a Single Role in a Block

Last modified: September 7, 2006 - 14:42

Using a similar SQL statement found in a page display of users by a certain role, Drupal can generate a block listing users and linking to their user profiles. This might be useful if you want to list a few users, such as an editorial team, in a block and not have to edit HTML each time to create that list. Replace the number 3 with the ID of the role you wish to display users from. You can find out the IDs of roles by going to administer » access control » roles tab and clicking the "edit" link. The ID appears at the end of the resulting URL.

Put this in a custom block (administer » blocks » add block tab) and be sure to select the "PHP code" input format.

<?php
$rid
= 3;
$result = db_query("SELECT u.uid, u.name, u.status FROM {users} u
INNER JOIN {users_roles} ur ON u.uid=ur.uid WHERE ur.rid = %d
AND u.status = 1"
, $rid);
while (
$u = db_fetch_object($result)) {
 
$items[] = l($u->name, "user/" . $u->uid);
}
return
theme('item_list', $items);
?>

To list the users alphabetically, use the following, slightly-modified code:

<?php
$rid
= 3;
$result = db_query("SELECT u.uid, u.name, u.status FROM {users} u
INNER JOIN {users_roles} ur ON u.uid=ur.uid WHERE ur.rid = %d
AND u.status = 1 ORDER BY u.name ASC"
, $rid);
while (
$u = db_fetch_object($result)) {
 
$items[] = l($u->name, "user/" . $u->uid);
}
return
theme('item_list', $items);
?>

Change "ASC" to "DESC" if you want the list reverse-alphabetical.

 
 

Drupal is a registered trademark of Dries Buytaert.