Who is Online and there Role

CreativeMe - July 4, 2007 - 19:13

Ok I would like to work if they are online and there role.
I would out how to list the roles with the code here.

<?php
print "<br>3";
$rid3 = 3;
$result = db_query("SELECT u.uid, u.name, u.status FROM {users} u
INNER JOIN {users_roles} ur ON u.uid=ur.uid WHERE ur.rid = %d
AND u.status = 1"
, $rid3);
while (
$u = db_fetch_object($result)) {
 
$items[] = l($u->name, "user/" . $u->uid);
}
print
theme('item_list', $items);
$items = "";
print
"<br>4";
$rid4 = 4;
$result = db_query("SELECT u.uid, u.name, u.status FROM {users} u
INNER JOIN {users_roles} ur ON u.uid=ur.uid WHERE ur.rid = %d
AND u.status = 1"
, $rid4);
while (
$u = db_fetch_object($result)) {
 
$items[] = l($u->name, "user/" . $u->uid);
print
theme('item_list', $items);
$items = "";
}
print
"<br>5";
$rid5 = 5;
$result = db_query("SELECT u.uid, u.name, u.status FROM {users} u
INNER JOIN {users_roles} ur ON u.uid=ur.uid WHERE ur.rid = %d
AND u.status = 1"
, $rid5);
while (
$u = db_fetch_object($result)) {
 
$items[] = l($u->name, "user/" . $u->uid);
print
theme('item_list', $items);
$items = "";
}
print
"<br>6";
$rid6 = 6;
$result = db_query("SELECT u.uid, u.name, u.status FROM {users} u
INNER JOIN {users_roles} ur ON u.uid=ur.uid WHERE ur.rid = %d
AND u.status = 1"
, $rid6);
while (
$u = db_fetch_object($result)) {
 
$items[] = l($u->name, "user/" . $u->uid);
print
theme('item_list', $items);
$items = "";
}
?>

Now I need to only display them if they are online in a session am losted how to do this with the sql.
If someone has the time would they mind helping me please.

Did you look at user.module

Tistur - July 4, 2007 - 23:56

Did you look at user.module (under user_block, about line 577 in 5.1)?

Also, is there a reason you're not using an array of role ids and looping through? Having so much repetitiveness could get very hairy if you need to change something.

I have looked at that code

CreativeMe - July 5, 2007 - 03:06

I have looked at that code yes
This is where I am at now.
[code]

<?php
print "Online Admin";
$rid3 = 3;
if (
user_access('access content')) {
         
// Count users with activity in the past defined period.
         
$time_period = variable_get('user_block_seconds_online', 900);
         
$result = db_query('
SELECT u.uid, u.name, u.status, u.access FROM (users) u INNER JOIN (users_roles) ur ON u.uid = ur.uid WHERE u.access >= %d AND u.uid != 0 AND ur.rid = %d AND u.status = 1 ORDER BY access DESC'
,time() - $time_period, $rid3);
while (
$u = db_fetch_object($result)) {
 
$items[] = l($u->name, "user/" . $u->uid);
?>

[/code]
I can not see where I have gone wrong?

I get this error Parse error: syntax error, unexpected $end in C:\xampplite\htdocs\d\includes\common.inc(1342) : eval()'d code on line 24

=-=

VeryMisunderstood - July 5, 2007 - 03:10

your parse error could be because you don't use any closing }'s

I've been using something

Tistur - July 5, 2007 - 05:00

I've been using something like this, but I'm not sure how a 3-way INNER JOIN would work.

<?php
    $interval
= time() - (600);
   
// Perform database queries to gather online user lists.  We use s.timestamp rather than u.access because it is much faster is much faster..
   
$active_users = db_query('SELECT u.uid, u.name FROM{users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.timestamp >= %d AND s.uid > 0  AND u.status != 0 ORDER BY s.timestamp DESC', $interval);
    while (
$account = db_fetch_object($active_users)) {
       
// etc....
?>

I had a go at making the

CreativeMe - July 5, 2007 - 13:46

I had a go at making the 3-way INNER JOIN but could not get it to work

[code]

<?php
print "Online rid 3";
$rid3 = 3;
$interval = time() - (600);
$active_users = db_query('SELECT u.uid, u.name FROM (users) u INNER JOIN (sessions) s ON (u.uid = s.uid) INNER JOIN (User_roles) ur ON (u.uid = ur.rid) WHERE ur.rid = 3 AND s.timestamp >= %d AND s.uid > 0  AND u.status != 0 ORDER BY s.timestamp DESC', $interval);
    while (
$account = db_fetch_object($active_users)) {
       
$active_users[] = l($u->name, "u.user/" . $u->u.uid);}
?>
[/code]

Unsure where I have gone wrong with this.

Got it!!

Tistur - July 16, 2007 - 01:15

You've probably found a better solution, but this seems to work for me (for future readers). It's extraordinarily wasteful and slow, but it works.

<?php
function _test_many_select(){
if (
user_access('access content')) {
   
$interval = time() - (5) * 60);

   
$users = db_query('SELECT u.name, u.uid, ur.rid FROM {users_roles} ur, {users} u WHERE u.uid IN (SELECT s.uid FROM {sessions} s WHERE timestamp >= %d) AND ur.uid = u.uid ORDER BY ur.rid', $interval);
   
    while (
$u = db_fetch_object($users)) {
       
$user_links[$u->uid] = l($u->name, "user/" . $u->uid);
       
$people[$u->rid][$u->uid] = & $user_links[$u->uid];
    }

   
$roles = db_query('SELECT * FROM {role} WHERE rid > %d',2);
   
    while(
$r = db_fetch_object($roles)){
       
$output .= '<b>'. $r->name .'</b>:<br>';
        foreach(
$people[$r->rid] as $uid => $person) {
           
$output .= $person .'<br>';
        }
    }
  }
    return   
$output;
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.