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.

Community
  • 1
  • 1
JeroenEijkhof
  • 2,232
  • 2
  • 25
  • 39
  • I'm curious, you stated that everyone told you that the easiest way to do this would be to use views, yet you're still not? If you did this with views you wouldn't need any code, except for a bit for advanced theming. If you are dead set on not using views, then look at how the users management page (Drupal core) pulls in the data. – Decipher Mar 02 '10 at 20:16
  • @Decipher: Thank you for the tip about the users management page, that was an interesting approach. – JeroenEijkhof Mar 02 '10 at 20:54
  • @Decipher: About views VS templates files. I actually said the opposite: "would be easier done without Views". – JeroenEijkhof Mar 02 '10 at 20:56
  • Sorry, I must have misread. As the sort of guy who uses code as much as possible, I would go the other way here, I think Views would be the easier option, considering it is basically just a SQL Query GUI. – Decipher Mar 03 '10 at 00:10

1 Answers1

1

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
  • 19,341
  • 1
  • 48
  • 64
  • 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. – JeroenEijkhof Mar 03 '10 at 06:14