Hi everybody,

I want to count et display the number of all users of such role (for example ID 2).

I don't understand what snippet of php code I must create to display this number.

Thanks for your help, it's important for my website.

Cédric

Comments

WorldFallz’s picture

This should work:

<?php
$roles = user_roles();
foreach ($roles as $rid => $name) {
  $result = db_query("SELECT COUNT(u.uid) as count FROM {users} u INNER JOIN {users_roles} ur ON
     u.uid=ur.uid WHERE ur.rid = %d AND u.status = 1", $rid);
  print "<br />" . $name . " = " . db_result($result);
}
?>

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

haopei’s picture

This works nicely in displaying user numbers of all roles WorldFallz.

However, can you give me a hint on how to display number of users of a certain role?

WorldFallz’s picture

something like this should do it:

$rid = 1;
$count = db_result(db_query("SELECT COUNT(u.uid) as count FROM {users} u INNER JOIN {users_roles} ur ON u.uid = ur.uid WHERE ur.rid = %d AND u.status = 1", $rid));
print "Administrators: " . $count;
Malthus’s picture


$role_id = 2;

$query = "
  SELECT COUNT(*)
  FROM {users} u INNER JOIN {users_roles} ur ON u.uid = ur.uid
  WHERE rid = %d;
";

$count = (db_result(db_query($query, $role_id)));

print "The number of users, with role id $role_id, is $count";

vstmusic’s picture

THANKS !

I try too to count the total number of the results of a view. Do you know what php code I must insert in the html code of my page ?

vstmusic’s picture

no idea ?

WorldFallz’s picture

http://drupal.org/project/views_calc

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

dkane’s picture

This is EXACTLY the snippet i'm looking for (I need to call it pragmatically in a rule) but it doesn't seem to work in D7. What changes would I need to make to this code to make it return a count of all users of a role in Drupal 7?

Thanks so much!