views:

57

answers:

1

I have an SQL query that lists the uid of all users who have a certain role:

SELECT u.uid 
FROM {users} as u, {users_roles} as ur 
WHERE u.uid = ur.uid AND ur.rid = 10 ORDER BY u.uid DESC

I need to load them all in an array as objects for listing.

I asked a question previously that left me only with the answer that what I wanted to do would be easier done without Views - so I'm going use a template file instead.
Therefore this question.

I know how to do this, but apparently my method is worth -2

This is how I want to do it:

$research['sql']   = "SELECT u.uid FROM {users} as u, {users_roles} as ur WHERE u.uid = ur.uid AND ur.rid = 10 ORDER BY u.uid DESC";
$research['sql_result'] = db_query($alumni['sql']);

// Load user objects and store in array
while($user_array = db_fetch_array($research['sql_result'])) {
  // Create user objets based on uid
  $user_obj = user_load($user_array['uid']);

  // Load profile
  profile_load_profile($user_obj);
  $research['users'][$user_obj->uid] = $user_obj;
}

Please help me with how I should do it.

+1  A: 

Your basic approach looks fine by me, except that the call to profile_load_profile() is redundant:

The user_load() function will invoke hook_user with operation 'load', and the profile module implements hook_user and calls profile_load_profile() for the load operation itself, so by the time you call it explicitly, it has already been called implicitly and you can just drop it.

Henrik Opel
THanks Henrik, i removed that line and it still works the exact same. For some reason I had the impression that none of my new profile fields would be added to the object if the `profile_load_profile()` wasn't called.
WmasterJ

related questions