• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Switch not going to last case

#1
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
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
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.