1

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.

Here is the table

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';
}

NOT THIS TOO

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

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

EDIT: 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';
                }
            }

EDIT 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/>';
}

NEW APPROACH

Please check this for new approach User online offline status - offline status issue

Community
  • 1
  • 1
Code Lover
  • 8,099
  • 20
  • 84
  • 154
  • 1
    Seems kinda whacky and illogical to compare the userid versus the $loggedtime, since they have nothing in common. Fix that and see what happens. – phpisuber01 Nov 02 '12 at 19:12
  • I have edited updated code can you please check and tel me where I am wrong now? – Code Lover Nov 02 '12 at 19:15

3 Answers3

1

Why not just check on the database side?

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

You can use TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2) to return datetime_expr2 – datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. One expression may be a date and the other a datetime; a date value is treated as a datetime having the time part '00:00:00' where necessary. The unit for the result (an integer) is given by the unit argument. The legal values for unit are the same as those listed in the description of the TIMESTAMPADD() function.

Take a look at the MySQL Date and Time Functions.

Also, I strongly advise using reserved words for table names.

Kermit
  • 33,827
  • 13
  • 85
  • 121
  • So if I use this query than do I have to check conditionally in php? or it will done with this query itself? Sorry but this type of code writing very first time – Code Lover Nov 02 '12 at 19:16
  • @pixelngrain See my updated query. If you use this, you do not have to do any checking in PHP. – Kermit Nov 02 '12 at 19:23
  • Okay let me try and get back to you in a moment – Code Lover Nov 02 '12 at 19:26
  • Please check my Edit 2 I am getting all user regardless the status. I am sorry I am really dumbo in this. please dont get frustrate – Code Lover Nov 02 '12 at 19:32
  • @pixelngrain Are you trying to show the status of users from the last 5 minutes? If yes, then what's the criteria that determines whether they are on **online** or **offline**? You also need to `echo $user[status] . '
    ';`
    – Kermit Nov 02 '12 at 19:36
  • Can we use session cookie etc? Actually myself confuse now that how to make this happen. :( Any suggestion or guideline? – Code Lover Nov 02 '12 at 19:39
  • @pixelngrain I have no idea how a session cookie comes into this. – Kermit Nov 02 '12 at 19:43
1

Your structure looks funny to answer the question. Your loggedin field actually looks more like a "the last time they logged in". Just because you know when they logged in doesn't necessarily mean they are "online".

The reason your query isn't working is because you are comparing a UNIX timestamp to a mysql datetime. In addition, you are using = so unless they logged in EXACTLY five minutes ago, this will not work.

At minimum.

SELECT userid, handle FROM ^users WHERE loggedin > '.date('Y-m-d h:i:s', time()-300).'ORDER BY....
jjs9534
  • 475
  • 2
  • 7
  • Oh wow.. thanks to notice me.. I 100% agree with you. So now how can I get status? – Code Lover Nov 02 '12 at 19:35
  • This is a much more complicated answer than this post allows, it would require additional fields, session checking, etc.... – jjs9534 Nov 02 '12 at 19:39
  • Do you mean by creating extra tables? – Code Lover Nov 02 '12 at 19:42
  • Not extra tables necessarily, but probably more robust than you current structure. It all depends on the project. – jjs9534 Nov 02 '12 at 19:43
  • Actually I want to display user list where it indicates online and offline status. I am using this open source http://www.question2answer.org. You can read some functions here if may helps http://www.question2answer.org/functions.php – Code Lover Nov 02 '12 at 19:45
  • It doesn't look like any of the functions in that list are going to help you with what you are trying to do. You may want to contact Q2A for support. I haven't personally done any work using them. – jjs9534 Nov 02 '12 at 19:50
  • Okay no problem.. Thanks for your help and effort. I appreciate it. – Code Lover Nov 02 '12 at 19:53
1

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

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.

phpisuber01
  • 7,585
  • 3
  • 22
  • 26
  • This is not rendering anything..! – Code Lover Nov 02 '12 at 19:22
  • Tested this code on my local machine here manually typing in your datetimes from above.. It works. – phpisuber01 Nov 02 '12 at 19:22
  • Yeh I can understand but somehow I am not getting it. these query function can cause the issue? `result = qa_db_query_sub($query); $users = qa_db_read_all_assoc($result);` – Code Lover Nov 02 '12 at 19:24
  • Look at your MySQL query... `SELECT userid, handle FROM ^users WHERE loggedin = '.$loggedtime.' ORDER BY userid ASC` This logic says in lamen: "Get me users where their login time is EQUAL TO the current time minus 5 minutes. You will never get a result unless you have a record with the accuracy of 1 second... – phpisuber01 Nov 02 '12 at 19:24
  • Take out that WHERE clause and see how it works: `SELECT userid, handle FROM ^users ORDER BY userid ASC` – phpisuber01 Nov 02 '12 at 19:26
  • I have taken out WHERE now it is displaying every user as a logged in user means online user. I think what @jjs9534 said that is correct and now I noticed that issue. – Code Lover Nov 02 '12 at 19:43
  • the point in removing the WHERE is to make sure the remainder of the code executes.. Good Luck. – phpisuber01 Nov 02 '12 at 19:44
  • Yes it is executing the code with all user in database with showing online status – Code Lover Nov 02 '12 at 19:46