Welcome Guest, Not a member yet? Register   Sign In
Switch not going to last case
#1

(This post was last modified: 07-07-2017, 05:50 AM by wolfgang1983.)

On my page I can see who's online and away. How every it does not switch to offline.

Does not go to the last switch case seems to stop at away How can I make sure switches offline correct.

I have set it up in short time intervals for testing 


PHP Code:
$questions_userinfo $this->question_model->get_user($question_info['question_user_id']);

$whosonlinetime time();

$online '';

switch (
$whosonlinetime) {

case 
$whosonlinetime $questions_userinfo['online'] + 300:

$online '<span class="text-success">[ONLINE]</span>';

break;

case 
$whosonlinetime $questions_userinfo['online'] + 400:

$online '<span class="text-warning">[AWAY]</span>';

break;

case 
$whosonlinetime $questions_userinfo['online'] + 600:

$online '<span class="text-danger">[OFFLINE]</span>';

break; 
}

$data['online'] = $online
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

(This post was last modified: 07-07-2017, 05:54 AM by rtenny.)

You are using a wrong logic here

$whosonlinetime > $questions_userinfo['online'] + 400:
$whosonlinetime > $questions_userinfo['online'] + 600:

if the value is 401 or GREATER the away condition is true
601 is also greater then 400 so the 400 condition is still true

your need something like this

$whosonlinetime > $questions_userinfo['online'] + 400 && $whosonlinetime < $questions_userinfo['online'] + 600:

or check for the +600 value first and then the +400
On the package it said needs Windows 7 or better. So I installed Linux.
Reply
#3

(This post was last modified: 07-07-2017, 08:08 AM by skunkbad.)

You could also just remove the break right after:

$online = '<span class="text-warning">[AWAY]</span>';

Also, please make use of indenting your code. It's hard to read now, and it'll be hard to read later.

PHP Code:
switch($x)
{
    case 
'a':
        
$y 1;
        break;
    case 
'b':
        
$y 2;
        break;
    case 
'c':
        
$y 3;
        break;
    default:
        
$y 4;

Reply
#4

PHP Code:
$diff =  time() - $questions_userinfo['online'];

if(
$diff >= 600) {
    
$class 'text-danger';
    
$text '[OFFLINE]';
} else if(
$diff >= 300) {
    
$class 'text-warning';
    
$text '[AWAY]';
} else if(
$diff >= 0) {
    
$class 'text-success';
    
$text '[ONLINE]';


$data['online'] = '<span class="'.$class.'">'.$text.'</span>'
Reply
#5

(07-07-2017, 10:19 AM)Paradinight Wrote:
PHP Code:
$diff  time() - $questions_userinfo['online'];

if(
$diff >= 600) {
    
$class 'text-danger';
    
$text '[OFFLINE]';
} else if(
$diff >= 300) {
    
$class 'text-warning';
    
$text '[AWAY]';
} else if(
$diff >= 0) {
    
$class 'text-success';
    
$text '[ONLINE]';


$data['online'] = '<span class="'.$class.'">'.$text.'</span>'

Thank you. And thanks to all others who helped. This worked fine.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB