List Users From a Single Role in a Block

Drupal 4.7.x - taken from Drupal site

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.

Comments

Final, Working Version

<P>This page is intended to give you an introduction to the Board of Directors.</P>
<P>The BOD conducts the business of Kappa Beta, such as securing a meeting room, reserving a dinner venue, and planning the annual Christmas party, to name a small part of it. They are not dictators. According to the Bylaws, they are supposed to lead by example. The BOD implements things, but the membership sets the direction by discussion and voting. Click here for more on <A HREF="WhatGood">What Good is the Board</A>.</P>

<?php
$rid
= 5;

print(
"<table><caption><b>The Board Members</b></caption>\n");

$header = array(
  array(
'data' => t('Username'), 'field' => 'u.name', 'sort' => 'asc'),
  array(
'data' => t('Photo')),
  array(
'data' => t('Name')),
  array(
'data' => t('Current Pos')),
  array(
'data' => t('CV')),
  array(
'data' => t('Member since'))
);

$sql = "SELECT u.uid, u.name 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);

while (
$account = db_fetch_object($result)) {
  
$account = user_load(array('uid' => $account->uid));
   if(
$account->picture){$account->picture = '<img src="/'.$account->picture.'" height="100" width="100" border="0" alt="'.$account->name.'">';}
     else{
$account->picture = '<img src="/files/pictures/KB/bodYou.gif" height="100" width="100" border="0" alt="no picture found">';}

print(
'<tr><td align="center">' . $account->picture . "<br>"
     
. $account->profile_firstname . " " . $account->profile_lastname
     
. '</td><td align="center"><b>' . $account->profile_currentboard . "</b>"
     
. "</td>\n<td>Member since " . date('F Y', mktime(0, 0, 0, $account->profile_date_joined['month'],
                                     
$account->profile_date_joined['day'],
                                     
$account->profile_date_joined['year']))
      .
". " . $account->profile_cv
     
. "</td></tr> \n");
}

print(
"</table>\n");
?>

Customising the user profile layout

Customising the user profile layout

The PHP Snippets below are intended for use within a customised USER PROFILE page that simply enables you to "pull" specific content from your drupal database specific to a particular user and display it in the way you want.

They are intended for use with a phptemplate based theme and for Drupal site developers who do not have php programming knowledge but want to push out the boundaries of user profile pages and control precisely how they look.

Simple step-by-step instructions are provided.