I am using drupal 6.2. I got the code for drupal 5.x ,the error is like that:

Status
Fatal error: Call to undefined function db_num_rows() in C:\AppServ\www\modules\user\user-profile.tpl.php on line 20

Anyone can make that work for drupal 6.x?

    print "<div class=\"fields\">";
   
        $time_period = variable_get('user_block_seconds_online', 600);
    $users = db_query("SELECT DISTINCT(uid), access FROM {users} WHERE access >= %d AND uid = %s", time() - $time_period, $user->uid);
    $total_users = db_num_rows($users);
        if ($total_users == 1)
        {
            $output = t($user->name.' is currently online');
        }
        else
        {
            $output = t($user->name.' is currently offline');     
        }
       
    print $output;
    print "</div>";

Comments

roopletheme’s picture

Szklana’s picture

thanks a lot.. :)

jax’s picture

By the way, you shouldn't replace the num_rows with the COUNT() solution mentioned in the other thready. The better solution is:

print "<div class=\"fields\">";
  
$time_period = variable_get('user_block_seconds_online', 600);
$users = db_query("SELECT DISTINCT(uid), access FROM {users} WHERE access >= %d AND uid = %s", time() - $time_period, $user->uid);
if (db_fetch_array($users))
{
  $output = t($user->name.' is currently online');
}
else
{
  $output = t($user->name.' is currently offline');    
}
    
print $output;
print "</div>";

Here you only do 1 query which either has a result or not. A COUNT() operation can be much more expensive.

Szklana’s picture

Ok it works.. but.. it only displays that I am online.. when I try to check other user profile it still displays like: Szklana is online.. and I would like to display if the user that I am checking is online ;S

Any idea?

C.Wolff’s picture

If anyone's still interested (since this was a topic of quite some time ago), you need to load the user that you're currently viewing. The above snippet works fine, but just need one adjustment:

<?php
$member = user_load(arg(1));
print "<div class=\"fields\">";
 
$time_period = variable_get('user_block_seconds_online', 600);
$users = db_query("SELECT DISTINCT(uid), access FROM {users} WHERE access >= %d AND uid = %s", time() - $time_period, $member->uid);
if (db_fetch_array($users))
{
  $output = t($member->name.' is online');
}
else
{
  $output = t($member->name.' is offline');
}
   
print $output;
print "</div>";
?> 

Now that you're loading the user that you're viewing (user_load(arg(1))), as long as you set block visibility to show on user and/or user/* , it'll load the profile you're viewing and output the appropriate status.