title: PHP: Online Offline Status
body: <p>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.</p>
  
  <p>Here is the table</p>
  
  <p><img src="https://i.stack.imgur.com/zV7AF.jpg" /></p>
  
  <p>and here what I tried to get status result.</p>
  
  <pre><code>$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'] &gt; $loggedtime){
      echo $row['handle'].' is online';
  } else {
      echo $row['handle'].' is offline';
  }
  </code></pre>
  
  <p><strong>NOT THIS TOO</strong></p>
  
  <pre><code>foreach($users as $user){
      if($user['userid'] &gt; $loggedtime){
          echo $user['handle']. ' is online';
      } else {
          echo $row['handle'].' is offline';
      }
  }
  </code></pre>
  
  <p>None of above code working. I am new to MYSQL and PHP just know basic so please help me to solve this.</p>
  
  <p><strong>EDIT:</strong>
  I have tried now this but not working</p>
  
  <pre><code>foreach($users as $user){
                  if($user['loggedin'] &gt; $loggedtime){
                      echo $user['handle']. ' is online';
                  } else {
                      echo $row['handle'].' is offline';
                  }
              }
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <pre><code>$query = "SELECT
      userid, handle, 
      CASE
          WHEN TIMESTAMPDIFF(SECOND, loggedin, NOW()) &lt; 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'] . '&lt;BR/&gt;';
  }
  </code></pre>
  
  <p><strong>NEW APPROACH</strong></p>
  
  <p>Please check this for new approach <a href="https://stackoverflow.com/questions/13208172/user-online-offline-status-offline-status-issue">User online offline status - offline status issue</a></p>