PHP:在线离线状态 [英] PHP: Online Offline Status

查看:33
本文介绍了PHP:在线离线状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

3 个小时以来,我一直在努力完成这项工作,但没有得到我想要的结果.我想显示在线和离线状态的用户列表.

Hi since 3 hour I am trying to make this work but not getting the result as I want. I want to display user list with online and offline status.

这是表

这里是我试图获得状态结果的内容.

and here what I tried to get status result.

$loggedtime = time() - 300; // 5 minutes

$query = 'SELECT userid, handle FROM ^users WHERE loggedin = '.$loggedtime.' ORDER BY userid ASC';

// below are scripts function qa_ pleses refer this http://www.question2answer.org/functions.php

$result = qa_db_query_sub($query);  
$users = qa_db_read_all_assoc($result); 

$row = mysql_fetch_array($result);

if($row['userid'] > $loggedtime){
    echo $row['handle'].' is online';
} else {
    echo $row['handle'].' is offline';
}

不是这个

foreach($users as $user){
    if($user['userid'] > $loggedtime){
        echo $user['handle']. ' is online';
    } else {
        echo $row['handle'].' is offline';
    }
}

以上代码均无效.我是 MYSQL 和 PHP 的新手,只知道基本的所以请帮我解决这个问题.

None of above code working. I am new to MYSQL and PHP just know basic so please help me to solve this.

我现在已经试过了,但没有用

I have tried now this but not working

foreach($users as $user){
                if($user['loggedin'] > $loggedtime){
                    echo $user['handle']. ' is online';
                } else {
                    echo $row['handle'].' is offline';
                }
            }

编辑 2

$query = "SELECT
    userid, handle, 
    CASE
        WHEN TIMESTAMPDIFF(SECOND, loggedin, NOW()) < 300
            THEN 'Online'
        ELSE 'Offline'
    END AS 'status'
FROM ^users
ORDER BY userid";

$result = qa_db_query_sub($query); 

while($user = mysql_fetch_array($result)){
    echo $user['handle'] . '<BR/>';
}

新方法

请检查此新方法用户在线离线状态 - 离线状态问题

推荐答案

既然你修复了用户 id 比较,让我们解决下一个问题..

Since you fixed the user id comparison, let's address the next issue..

您正在尝试将字符串 DATE 与 unix 时间戳进行比较.让我们将它们设为相同类型并进行比较:

You're trying to compare a string DATE versus a unix timestamp. Let's make them the same type and compare:

foreach($users as $user)
{
  $user_time = strtotime($user['loggedin']);
  if($user_time > $loggedtime)
  {
    echo $user['handle']. ' is online';
  } else {
    echo $row['handle'].' is offline';
  }
}

总的来说,这不是解决此问题的最佳方法,但它可能会为您工作.上面的数据库解决方案可能是最好的.

Overall not the best way to approach this problem, but it might get this working for you. The database solution above is probably best.

这篇关于PHP:在线离线状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆