rofl90 Posted March 20, 2008 Share Posted March 20, 2008 Ok so, i'm trying to calculate whether the user is online, offline, or about to be timed out by systems (300 sec off being timed out), every page i update with a new timestamp, and then on this screen I get time() and - it by the old timestamp and calculate from there heres my code: $result = mysql_query("SELECT * FROM users") or die(mysql_error()); echo "<table cellspacing='4' cellpadding='2'>"; echo "<tr> <th bgcolor='#CCCCCC'>Username</th> <th bgcolor='#CCCCCC'>Online?</th> <th bgcolor='#CCCCCC'>Ban!</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td bgcolor='#E5E5E5'><div align='center'><a href='usermanagement.php?user="; echo $row['user']; echo "'>"; echo $row['user']; echo "</a>"; echo "</div></td><td bgcolor='#EBEBEB'><div align='center'>"; $userid = $row['id']; $checkonline = mysql_query("SELECT * FROM session WHERE userid='$userid'"); $checkonline_a = mysql_fetch_array($checkonline); $timeout = "900"; $now = time (); $nearly = "300"; $last = $checkonline_a['date']; $check = $now - $last; if ($check > $timeout) { echo "<img src=\"http://"; echo $_SERVER['HTTP_HOST']; echo "/backend/images/offline.png\" />"; } elseif($check > $nearly && $check > $timeout) { echo "<img src=\"http://"; echo $_SERVER['HTTP_HOST']; echo "/backend/images/timeout.png\" />"; } else { echo "<img src=\"http://"; echo $_SERVER['HTTP_HOST']; echo "/backend/images/online.png\" />"; } if($row['banned'] == 1) { echo "</div></td><td bgcolor='#EAEBCB'><div align='center'>"; echo "<a href='unbanuser.php?user="; echo $row['user']; echo "'>Unban</a>"; } else { echo "</div></td><td bgcolor='#EAEBCB'><div align='center'>"; echo "<a href='banuser.php?user="; echo $row['user']; echo "'>Ban</a>"; } echo "</div></td></tr>"; } echo "</table>"; echo "<br />"; echo "<input type=\"button\" name=\"new\" id=\"add\" value=\"Add User\" onclick=\"parent.location='adduser.php'\" />"; Quote Link to comment https://forums.phpfreaks.com/topic/97029-logic-is-wrong/ Share on other sites More sharing options...
Jeremysr Posted March 20, 2008 Share Posted March 20, 2008 Try changing elseif($check > $nearly && $check > $timeout) { to elseif($check > $nearly) { Quote Link to comment https://forums.phpfreaks.com/topic/97029-logic-is-wrong/#findComment-496534 Share on other sites More sharing options...
rofl90 Posted March 20, 2008 Author Share Posted March 20, 2008 Hmm, i fixed it but my new logic is still wrong, it's providing me with when online: - orange, when offline: offline and online. :S heres my new code: $result = mysql_query("SELECT * FROM users") or die(mysql_error()); echo "<table cellspacing='4' cellpadding='2'>"; echo "<tr> <th bgcolor='#CCCCCC'>Username</th> <th bgcolor='#CCCCCC'>Online?</th> <th bgcolor='#CCCCCC'>Ban!</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td bgcolor='#E5E5E5'><div align='center'><a href='usermanagement.php?user="; echo $row['user']; echo "'>"; echo $row['user']; echo "</a>"; echo "</div></td><td bgcolor='#EBEBEB'><div align='center'>"; $userid = $row['id']; $checkonline = mysql_query("SELECT * FROM session WHERE userid='$userid'"); $checkonline_a = mysql_fetch_array($checkonline); $timeout = "900"; $now = time (); $nearly = "300"; $last = $checkonline_a['date']; $check = $now - $last; if ($check > $timeout) { echo "<img src=\"http://"; echo $_SERVER['HTTP_HOST']; echo "/backend/images/offline.png\" />"; } if($check < $nearly) { echo "<img src=\"http://"; echo $_SERVER['HTTP_HOST']; echo "/backend/images/timeout.png\" />"; } else { echo "<img src=\"http://"; echo $_SERVER['HTTP_HOST']; echo "/backend/images/online.png\" />"; } if($row['banned'] == 1) { echo "</div></td><td bgcolor='#EAEBCB'><div align='center'>"; echo "<a href='unbanuser.php?user="; echo $row['user']; echo "'>Unban</a>"; } else { echo "</div></td><td bgcolor='#EAEBCB'><div align='center'>"; echo "<a href='banuser.php?user="; echo $row['user']; echo "'>Ban</a>"; } echo "</div></td></tr>"; } echo "</table>"; echo "<br />"; echo "<input type=\"button\" name=\"new\" id=\"add\" value=\"Add User\" onclick=\"parent.location='adduser.php'\" />"; Quote Link to comment https://forums.phpfreaks.com/topic/97029-logic-is-wrong/#findComment-496542 Share on other sites More sharing options...
Jeremysr Posted March 20, 2008 Share Posted March 20, 2008 The if statements should be this: echo "<img src=\"http://"; echo $_SERVER['HTTP_HOST']; if ($check > $timeout) { echo "/backend/images/offline.png\" />"; } else if ($check > $nearly) { echo "/backend/images/timeout.png\" />"; } else { echo "/backend/images/online.png\" />"; } I got rid of some unnecessary repetition too. (Exact same code was in all three blocks.) Quote Link to comment https://forums.phpfreaks.com/topic/97029-logic-is-wrong/#findComment-496545 Share on other sites More sharing options...
rofl90 Posted March 20, 2008 Author Share Posted March 20, 2008 Ah.. gotcha, I realised instead of the 'nearly' being 300 as if 300 sec left say there nearly timed out it should be 600 in relation to 900, as its BIGGER.. thanks for the cleaner code too. [solved] Quote Link to comment https://forums.phpfreaks.com/topic/97029-logic-is-wrong/#findComment-496549 Share on other sites More sharing options...
sasa Posted March 20, 2008 Share Posted March 20, 2008 try <?php if ($check > $timeout) { echo "<img src=\"http://"; echo $_SERVER['HTTP_HOST']; echo "/backend/images/offline.png\" />"; } else if($check > $nearly) { echo "<img src=\"http://"; echo $_SERVER['HTTP_HOST']; echo "/backend/images/timeout.png\" />"; } else { echo "<img src=\"http://"; echo $_SERVER['HTTP_HOST']; echo "/backend/images/online.png\" />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/97029-logic-is-wrong/#findComment-496561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.