Hi, using Drupal 5.x...please help if you have insight.

I would like to, based on user role, use a different query in my module to display results.

For example.

If role "administrator"

$query = "SELECT * FROM `table`;

If role "anonymous user"

$query = "SELECT * FROM `table` WHERE 'public'=1;

Any thoughts on how to go about this?

Comments

kitt’s picture

Use the $user->roles array.

// if not declared in your function yet...
global $user;

if (in_array('administrator', $user->roles)) {
  // admin
  $q = 'select * from ...';
} else if (in_array('editor', $user->roles)) {
  // editor
  $q = 'select * from ... something else';
} else if ($user->uid == 0) {
  // anonymous / not logged in
  $q = 'select * from ...';
}

Though, if you have more anonymous users, you may want that $user->uid == 0 check first, to save you (a small bit of) processor time.

stoob’s picture

Thank you this worked great. At first I got an error because global $user was not in the function. I got a "wrong datatype" warning when this was the case. Adding $user to the function fixed that. That was my only small problem. Thanks for the help.