How to create a page that lists all users and profile fields
Last modified: July 8, 2009 - 04:35
Copied from this comment, http://drupal.org/node/144037#comment-555339
In Drupal 5, Views did not handle users at all. This feature was added to Views 2 with Drupal 6.
So to create a page that lists all users, image and their profile fields on your Drupal website you need to:
1. Create a new page
2. Insert the following page text as PHP code.
3. Make sure the input format is set to PHP
This code will list the users in name alphabetical order, 50 per page.
<?php
// Compile a list of fields to show.
$fields = array();
$result = db_query('SELECT name, title, type, weight FROM {profile_fields} WHERE visibility = %d ORDER BY category, weight', PROFILE_PUBLIC_LISTINGS);
while ($record = db_fetch_object($result)) {
$fields[] = $record;
}
// v.fid = 1 is the include in member gallery checkbox
// If the field contains integer values use ORDER BY ABS(v.value) to force numeric sorting
//Change the 50 to display more or less users per page
$result = pager_query("SELECT u.uid, u.access FROM {users} u INNER JOIN {profile_values} v ON u.uid = v.uid WHERE v.fid = 1 ORDER BY u.name, v.value ASC", 50, 0, NULL);
$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);
return $output;
?>
Display All Public Profile Information
Thank you for this great tip! I just have one thing to add: If you want the new page to list the full public profile of each user (instead of just the user's userid and full name), then remove the "WHERE visibility = %d " phrase from the 4th line of the code above. So, your final fourth line should read:
$result = db_query('SELECT name, title, type, weight FROM {profile_fields} ORDER BY category, weight', PROFILE_PUBLIC_LISTINGS);