Custom User Blocks and User Tables PHP Snippets
Custom User Blocks and User Tables PHP Snippets
Tested in Drupal 4.7.4 and 4.7.5
These are generic scripts that should work in other Drupal versions too.
The following are some PHP Snippets for Custom User Blocks and User Tables (Member Lists) extracted various forums within the Drupal.org site.
I modified some of the Snippets, as indicated in the sections below, to also include and output user personal information profile fields and their corresponding profile photos.
I am posting them here on one page so that other users may utilize them.
Sam Raheb (Sam308)
------------------------------------------------------------------------------
(1) Custom Who's New Block
Outputs User's Name [all roles]
------------------------------------------------------------------------------
<?php
$result = db_query_range('SELECT uid FROM {users} WHERE status != 0 ORDER BY uid DESC', 0, 8);
$items = array();
while ($row = db_fetch_object($result)) {
$account = user_load(array('uid' => $row->uid));
$items[] = check_plain($account->name);
}
return theme('item_list', $items);
?>The above PHP Snippet code yields the following results:
• fourmu
• dicema
• Debbie
• Sammy
• TestUser
• Jimmy
• adminwhere the Usernames are not hyperlinks
------------------------------------------------------------------------------
(2) Custom Who's New Block
Outputs User's Real Name [all roles]
(see Home » administer » settings » profiles)
------------------------------------------------------------------------------
<?php
$result = db_query_range('SELECT uid FROM {users} WHERE status != 0 ORDER BY uid DESC', 0, 8);
$items = array();
while ($row = db_fetch_object($result)) {
$account = user_load(array('uid' => $row->uid));
$items[] = check_plain($account->profile_real_name);
}
return theme('item_list', $items);
?>The above PHP Snippet code yields the following results:
• Barbara Miller
• Michelle
• Debbie Walker
• Sam Raheb
• Test User
• James Thompson
• Sam Rahebwhere the Real Names are not hyperlinks
------------------------------------------------------------------------------
(3) List a User's single personal profile field for a Single Role
------------------------------------------------------------------------------
<?php
global $user;
$sql = "SELECT value FROM profile_values WHERE (uid = $user->uid) AND (fid = 2)"; //'2' is MY location field... it may not be the same as yours.'
$result = db_query($sql);
$location = db_fetch_object($result);
print "<br> $location->value";
?>The above PHP Snippet code yields the following results:
Business Owner------------------------------------------------------------------------------
(4) List Users From a Single Role in a Block
Outputs User's Name (http://drupal.org/node/82002)
------------------------------------------------------------------------------
<?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);
?>The above PHP Snippet code yields the following results:
• admin
• Debbie
• dicema
• Jimmy
• Sammywhere the usernames are hyperlinks to the user's profile.
------------------------------------------------------------------------------
(5) List Users From a Single Role
Output is the typical Profile Listing Style - not a table
------------------------------------------------------------------------------
<?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
$fields = array();
$result = db_query('SELECT name, title, type, weight FROM {profile_fields} WHERE visibility = %d ORDER BY category DESC, weight', PROFILE_PUBLIC_LISTINGS);
while ($record = db_fetch_object($result)) {
$fields[] = $record;
}
$result = pager_query("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 ORDER BY access DESC", 20, 0, NULL);
$status = array(t('blocked'), t('active'));
$output = '<div id="profile">';
while ($account = db_fetch_object($result)) {
$account = user_load(array('uid' => $account->uid));
$profile = _profile_update_user_fields($fields, $account);
$output .= theme('profile_listing', $account, $profile);
}
$output .= '</div>';
$output .= theme('pager', NULL, 20);
print ($output);
?>------------------------------------------------------------------------------
(6) List Users From a Single Role and "Includes Edit field"
Outputs User Table (http://drupal.org/node/63422)
------------------------------------------------------------------------------
<?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'),
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);
?>The above PHP Snippet code yields the following results:
Username Status Member for Last access Operations
--------------------------------------------------------------------------------
admin active 1 week 19 hours 4 sec ago edit
Debbie active 11 hours 19 min never edit
Jimmy active 5 days 23 hours 9 hours 41 min ago edit
Sammy active 4 days 8 hours 32 min 35 sec ago edit------------------------------------------------------------------------------
(7) List Users From a Single Role - "No Edit field"
Outputs User Table (http://drupal.org/node/63422)
------------------------------------------------------------------------------
<?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);
?>The above PHP Snippet code yields the following results:
Username Status Member for Last access
--------------------------------------------------------------------
Debbie active 11 hours 19 min never
Sammy active 4 days 8 hours 32 min 35 sec ago
Jimmy active 5 days 23 hours 9 hours 41 min ago
admin active 1 week 19 hours 4 sec ago ------------------------------------------------------------------------------
(8) List Users from a Single Role and includes a single personal profile field augmented to the Username
Outputs User Table (http://drupal.org/node/63422)
------------------------------------------------------------------------------
<?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)) {
$account = user_load(array('uid' => $account->uid));
$account->name .= ' (' . $account->profile_profession . ')';
$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);
?>The above PHP Snippet code yields the following results:
Username Status Member for Last access
-----------------------------------------------------------------------------
admin (Business... active 1 week 19 hours 4 sec ago
Debbie (Execut... active 11 hours 19 min never
Jimmy (Business O. active 5 days 23 hours 9 hours 41 min ago
Sammy (Business... active 4 days 8 hours 32 min 35 sec ago ------------------------------------------------------------------------------
(9) List Users from a "Single Role" and includes multiple personal profile data - I modified the above script (8) to also output personal information profile filelds.
Outputs User Table (http://drupal.org/node/103497)
------------------------------------------------------------------------------
Where the following are Personal Profile Fields:
------------------------------------------------
Profession = profile_profession
Membership = profile_membership
Gender = profile_gender
Age = profile_age
Zip code = profile_zipcode
<?php
//choose the role to list by value.
// Note ID 1 = anonymous, ID 2 = authenticated user, ID 3 = member, ID 4 = admin-level1
// so valid values here are > 2.
$rid = 3;
?>
<br/>
Click on the Field name to sort the list.<br/><br>
Currently the fields: Profession, Membership, Gender, Age, Hobbies & Interest, and Zip code, are not sortable.
<?php
$header = array(
array('data' => t('Username'), 'field' => 'u.name', 'sort' => 'asc'),
array('data' => t('User'), 'field' => 'u.uid'),
array('data' => t('Profession'), 'field' => 'u.profession'),
array('data' => t('Membership'), 'field' => 'u.membership'),
array('data' => t('Gender'), 'field' => 'u.gender'),
array('data' => t('Age'), 'field' => 'u.age'),
array('data' => t('Zip code'), 'field' => 'u.zipcode'),
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)) {
$account = user_load(array('uid' => $account->uid));
$rows[] = array(theme('username', $account),
$account->uid,
$account->profile_profession,
$account->profile_membership,
$account->profile_gender,
$account->profile_age,
$account->profile_zipcode,
$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);
?>The above PHP Snippet code yields the following results:
Note: Only the fields: Ussername, User, Status, Member for, and Last access, are sortable.
This table may wrap and not appear correctly on this drupal.org handbook page, but will display correctly on your website. You can also copy and paste this table into an ASCII document to view it correctly.
Username User Profession Membership Gender Age Zip code Status Member for Last access
----------------------------------------------------------------------------------------------------------------------------
admin 1 Business Owner A-Member Male 41 91324 active 1 week 19 hours 4 sec ago
Debbie 6 Executive A-Member Female 38 91377 active 11 hours 19 min never
Jimmy 2 Business Owner B-Member Male 24 91405 active 5 days 23 hours 9 hours 41 min ago
Sammy 4 Business Owner Non-member Male 37 97651 active 4 days 8 hours 32 min 35 sec ago ------------------------------------------------------------------------------
(10) List Users from a "ALL Roles" and includes multiple personal profile data - I modified the above script (9) to also output both the personal information profile fields and the profile images.
Outputs User Table (http://drupal.org/node/103497)
------------------------------------------------------------------------------
This Members list displays a table of "all users" from "all roles" including their profile images which are links.
Where the following are Personal Profile Fields:
------------------------------------------------
Membership = profile_membership
Zip code = profile_zipcode
Age = profile_age
Gender = profile_gender
Profession = profile_profession
Note:
Remember to replace the default image and path below (../assets/images/default-user-sm.gif) with your default image and path.
<?php
// Displays a list of all users including their profile images (all roles)
$header = array(
array('data' => t('Username'), 'field' => 'u.name', 'sort' => 'asc'),
array('data' => t('Photo'), 'field' => 'u.photo'),
array('data' => t('Membership'), 'field' => 'u.membership'),
array('data' => t('Zip code'), 'field' => 'u.zipcode'),
array('data' => t('Age'), 'field' => 'u.age'),
array('data' => t('Gender'), 'field' => 'u.gender'),
array('data' => t('Profession'), 'field' => 'u.profession'),
array('data' => t('Last access'), 'field' => 'u.access')
);
$sql = 'SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u WHERE uid != 0';
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
$status = array(t('blocked'), t('active'));
while ($account = db_fetch_object($result)) {
$account = user_load(array('uid' => $account->uid));
if($account->picture){$account->picture = '<a href="?q=user/'.$account->uid.'"><img src="'.$account->picture.'" height="25" width="25" border="1" alt=""></a>';}
else{$account->picture = '<a href="?q=user/'.$account->uid.'"><img src="../assets/images/default-user-sm.gif" height="25" width="25" border="1" alt=""></a>';}
$rows[] = array(theme('username', $account),
$account->picture,
$account->profile_membership,
$account->profile_zipcode,
$account->profile_age,
$account->profile_gender,
$account->profile_profession,
$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);
?>The above PHP Snippet code yields the following results:
See an actual screen shot at: http://xlecom.com/assets/images/example-member-list.gif
Note: Only the fields: Ussername and Last access are sortable.
This table may wrap and not appear correctly on this drupal.org handbook page, but will display correctly on your website. You can also copy and paste this table into an ASCII document to view it correctly.
Username Photo Membership Zip code Age Gender Profession Last access
-----------------------------------------------------------------------------------------------------
admin (photo) Member 95421 47 Male Business Owner 6 min 51 sec ago
bachel (photo) Member 95488 46 Male Warehouse Supervisor 12 hours 21 min ago
cristo (photo) Member 90095 43 Male Career Counselor 5 days 9 hours ago
Debbie (photo) Member 95481 42 Female Executive never
dicema (photo) Member 95450 40s Female Investigations 2 weeks 2 days ago
fourmu (photo) Non-Member 95421 44 Female Self Employed 1 week 2 days ago
Jimmy (photo) Member 95450 28 Male Business Owner 1 day 14 hours ago
michael (photo) Non-Member 95481 53 Male Public Affairs 1 day 14 hours ago
Sammy (photo) Member 95421 47 Male Business Owner 8 hours 40 min ago
soulhea (photo) Member 95420 42 Female Business owner never
TestUs (photo) Member 95421 33 Male Self Employeed 11 hours 5 min ago------------------------------------------------------------------------------
(11) List Users from a "ALL Roles" and includes their "ROLES", personal profile data, profile images, and Edit field.
This Table is meant to be used only by Site Administrators because of the inclusion of the Roles and the Edit fields.
This is a modification of script (10)
Outputs User Table
------------------------------------------------------------------------------
This Members list displays a table of "all users" from "all roles" including their "ROLES", personal profile data, profile images, and Edit field. The thumbnail profile images are also links to the user's profile page.
The following are the Personal Profile Fields:
----------------------------------------------
Membership = profile_membership
Zip code = profile_zipcode
Age = profile_age
Gender = profile_gender
Profession = profile_profession
Note:
Remember to replace the default image and path below (../assets/images/default-user-sm.gif) with your default image and path.
<?php
// Displays a list of all users including their Roles, personal profile data, profile images, and Edit field. (all roles)
$header = array(
t('Edit'),
array('data' => t('Username'), 'field' => 'u.name', 'sort' => 'asc'),
array('data' => t('User ID'), 'field' => 'u.uid'),
array('data' => t('Photo')),
array('data' => t('Roles')),
array('data' => t('Membership')),
array('data' => t('Zip code')),
array('data' => t('Age')),
array('data' => t('Gender')),
array('data' => t('Profession')),
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 WHERE uid != 0';
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
$status = array(t('blocked'), t('active'));
$destination = drupal_get_destination();
while ($account = db_fetch_object($result)) {
$rolesresult = db_query('SELECT r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d', $account->uid);
$roles = array();
$account = user_load(array('uid' => $account->uid));
if($account->picture){$account->picture = '<a href="?q=user/'.$account->uid.'"><img src="'.$account->picture.'" height="25" width="25" border="1" alt=""></a>';}
else{$account->picture = '<a href="?q=user/'.$account->uid.'"><img src="../assets/images/default-user-sm.gif" height="25" width="25" border="1" alt=""></a>';}
while ($role = db_fetch_object($rolesresult)) {
$roles[] = $role->name;
}
$rows[] = array(
l(t('edit'), "user/$account->uid/edit", array()),
theme('username', $account),
$account->uid,
$account->picture,
implode(',<br />', $roles),
$account->profile_membership,
$account->profile_zipcode,
$account->profile_age,
$account->profile_gender,
$account->profile_profession,
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);
?>The above PHP Snippet code yields the following results:
Note: Only the fields: "Ussername", "User ID", "Member for", and "Last access" are sortable.
This table may wrap and not appear correctly on this drupal.org handbook page, but will display correctly on your website. You can also copy and paste this table into an ASCII document to view it correctly.
Edit Username User ID Photo Roles Membership Zip code Age Gender Profession Member for Last access
-------------------------------------------------------------------------------------------------------------------------------------------------
edit admin 1 (photo) Employee Member 95421 47 Male Business Owner 10 weeks 16 hours 6 min 51 sec ago
edit bachel 4 (photo) Employee Member 95488 46 Male Warehouse Supervisor 7 weeks 6 days 12 hours 21 min ago
edit cristo 6 (photo) Employee Member 90095 43 Male Career Counselor 5 days 11 hours 5 days 9 hours ago
edit Debbie 11 (photo) Manager Member 95481 42 Female Executive 1 weeks 4 days never
edit dicema 5 (photo) Manager Member 95450 40s Female Investigations 6 weeks 18 hours 2 weeks 2 days ago
edit fourmu 10 (photo) Employee Non-Member 95421 44 Female Self Employed 1 weeks 4 days 1 week 2 days ago
edit Jimmy 8 (photo) Manager Member 95450 28 Male Business Owner 3 weeks 4 days 1 day 14 hours ago
edit michael 9 (photo) Employee Non-Member 95481 53 Male Public Affairs 2 weeks 4 days 1 day 14 hours ago
edit Sammy 2 (photo) Manager Member 95421 47 Male Business Owner 9 weeks 4 days 8 hours 40 min ago
edit soulhea 7 (photo) Manager Member 95420 42 Female Business owner 4 weeks 4 days never
edit TestUs 3 (photo) Employee Member 95421 33 Male Self Employeed 8 weeks 4 days 11 hours 5 min ago------------------------------------------------------------------------------
(12) An IF-THEN version of script for approved user roles. List Users from a "ALL Roles" and includes multiple personal profile data.
Outputs a User Table similar to example (10)
------------------------------------------------------------------------------
Where the following are Personal Profile Fields:
------------------------------------------------
Zip code = profile_zipcode
Age = profile_age
Gender = profile_gender
Profession = profile_profession
Location = profile_location
Where the Approved Roles are:
-----------------------------
admin-level1
authenticated user
member
pre-authorized
Note:
Remember to replace the default image and path below (../assets/images/default-user-sm.gif) with your default image and path.
Below is the basic IF-THEN script for approved user roles. This is the same as the script below, but without the inserted Member's List PHP code for the TRUE and FALSE conditions.
This version uses an IF-THEN routine to determine the user's role. If the user is a site member (approved roles), then the script will execute the TRUE condition. If not a site member (Anonymous user), then the script will will execute the FALSE condition.
<?php
global $user;
$output ='';
$approved_roles = array('admin-level1', 'authenticated user', 'member', 'pre-authorized');
if (is_array($user->roles)) {
if (count(array_intersect($user->roles, $approved_roles)) > 0)
{place your PHP code here for a TRUE condition - for the approved roles}
else
{place your PHP code here for a FALSE condition - for none of the approved roles}
}
?>A Working Example:
This version uses an IF-THEN routine to determine the user's role. If the user is a site member (approved roles), then the page will display a Member's List with valid usernames and thumbnail photos. If not a site member (Anonymous user), then the page will display the same Member's List using fake usernames and no real photos.
Below is the IF-THEN version which first checks the role of the site visitor and displays the appropriate Member's List:
In the sample code below, I removed some of the "print" HTML statements and two user fields that I am actually using in order to display a shorter version of the script.
<?php
global $user;
$approved_roles = array('admin-level1', 'authenticated user', 'member', 'pre-authorized');
if (is_array($user->roles)) {
if (count(array_intersect($user->roles, $approved_roles)) > 0)
{
// Actual Site Members
print("<P style=\"TEXT-ALIGN: justify\"><B>Site Members</b><br>The following is a list of all site members.</p>");
print("<P style=\"TEXT-ALIGN: justify\"><i>Currently, only the <span style=\"color: rgb(153,0,0)\">Ussername</span> field is sortable.</i></p>");
// Displays a list of all Actual Site Members (all roles)
$header = array(
array('data' => t('Username'), 'field' => 'u.name', 'sort' => 'asc'),
array('data' => t('Photo')),
array('data' => t('Zip code')),
array('data' => t('Age')),
array('data' => t('Gender')),
array('data' => t('Profession')),
array('data' => t('Location'))
);
$sql = 'SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u WHERE uid != 0';
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
$status = array(t('blocked'), t('active'));
while ($account = db_fetch_object($result)) {
$account = user_load(array('uid' => $account->uid));
if($account->picture){$account->picture = '<a href="?q=user/'.$account->uid.'"><img src="'.$account->picture.'" height="25" width="25" border="1" alt=""></a>';}
else{$account->picture = '<a href="?q=user/'.$account->uid.'"><img src="../assets/images/default-user-sm.gif" height="25" width="25" border="1" alt=""></a>';}
$rows[] = array(theme('username', $account),
$account->picture,
$account->profile_zipcode,
$account->profile_age,
$account->profile_gender,
$account->profile_profession,
$account->profile_location
);
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 50, 0);
print ($output);
}
else{
// Sample List of Site Members
print("<P style=\"TEXT-ALIGN: justify\"><B>Sample List of Site Members</b><br>The following is an actual sample list of our site members. Their \"real\" usernames and photos are <u>only available to registered site members</u>. After you log in, this list will include both their \"real\" usernames and photos. You will also have the option to <span style=\"color: rgb(153,0,0)\">sort</span> the list</p>");
// Displays a Sample List of Site Members (all roles)
$header = array(
array('data' => t('Username')),
array('data' => t('Photo')),
array('data' => t('Zip code')),
array('data' => t('Age')),
array('data' => t('Gender')),
array('data' => t('Profession')),
array('data' => t('Location'))
);
$sql = 'SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u WHERE uid != 0';
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
$status = array(t('blocked'), t('active'));
while ($account = db_fetch_object($result)) {
$account = user_load(array('uid' => $account->uid));
$account->picture = '<img src="../assets/images/default-user-sm.gif" height="25" width="25" border="1" alt=""></a>';
srand((double)microtime()*1000000);
$randnum = rand(0,1000);
$fakeuser = "User-" . $randnum;
$rows[] = array($fakeuser,
$account->picture,
$account->profile_zipcode,
$account->profile_age,
$account->profile_gender,
$account->profile_profession,
$account->profile_location
);
}
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 50, 0);
print ($output);
}
}
?>------------------------------------------------------------------------------
(13) An IF-THEN version of script for approved user roles. List Users from a "ALL Roles" and includes multiple personal profile data.
Outputs a User Table similar to example (10).
This version outputs a "sortable list by all fields" by utilizing the Members module.
------------------------------------------------------------------------------
Members module: http://drupal.org/project/members (version tested: members-4.7.x-1.x-dev.tar.gz)
This example is essentially the same are example (12), but uses the Members module to create a "sortable list by all fields". Approved Role members can sort the list by field title in ascending or descending order and view the profiles of all site members who share a common response in a field category by clicking on the links within the table itself.
Where the following are Personal Profile Fields:
------------------------------------------------
Zip code = profile_zipcode
Age = profile_age
Gender = profile_gender
Profession = profile_profession
Location = profile_location
Where the Approved Roles are:
-----------------------------
admin-level1
authenticated user
member
pre-authorized
A Working Example:
This version uses an IF-THEN routine to determine the user's role. If the user is a site member (approved roles), then the page will display a Member's List with valid usernames and thumbnail photos. If not a site member (Anonymous user), then the page will display the same Member's List using fake usernames and no real photos.
The following PHP code is used for the TRUE condition. It calls the Members list module to display the members list:
$q = $_GET['q'];
$_GET['q'] = 'members';
print menu_execute_active_handler();
$_GET['q'] = $q;By the way, this piece of code is useful for retrieving any page or URL in the Drupal system.
To see a working version, go to: http://singleinscv.com/ and click on the "Site Members" link.
Below is the IF-THEN version which first checks the role of the site visitor and displays the appropriate Member's List:
In the sample code below, I removed some of the "print" HTML statements and two user fields that I am actually using in order to display a shorter version of the script.
<?php
global $user;
$approved_roles = array('admin-level1', 'authenticated user', 'member', 'pre-authorized');
if (is_array($user->roles)) {
if (count(array_intersect($user->roles, $approved_roles)) > 0)
{
// Actual Site Members
print("<P style=\"TEXT-ALIGN: justify\"><B>Site Members</b><br>The following is a list of all site members.</p>");
print("<P style=\"TEXT-ALIGN: justify\"><i>Currently, only the <span style=\"color: rgb(153,0,0)\">Ussername</span> field is sortable.</i></p>");
// Displays a list of all Actual Site Members (all roles)
$q = $_GET['q'];
$_GET['q'] = 'members';
print menu_execute_active_handler();
$_GET['q'] = $q;
}
else{
// Sample List of Site Members
print("<P style=\"TEXT-ALIGN: justify\"><B>Sample List of Site Members</b><br>The following is an actual sample list of our site members. Their \"real\" usernames and photos are <u>only available to registered site members</u>. After you log in, this list will include both their \"real\" usernames and photos. You will also have the option to <span style=\"color: rgb(153,0,0)\">sort</span> the list</p>");
// Displays a Sample List of Site Members (all roles)
$header = array(
array('data' => t('Username')),
array('data' => t('Photo')),
array('data' => t('Zip code')),
array('data' => t('Age')),
array('data' => t('Gender')),
array('data' => t('Profession')),
array('data' => t('Location'))
);
$sql = 'SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u WHERE uid != 0';
$sql 