Jump to content

Logic is wrong.


rofl90

Recommended Posts

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'\"  />";

Link to comment
https://forums.phpfreaks.com/topic/97029-logic-is-wrong/
Share on other sites

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'\"  />";

Link to comment
https://forums.phpfreaks.com/topic/97029-logic-is-wrong/#findComment-496542
Share on other sites

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.)

Link to comment
https://forums.phpfreaks.com/topic/97029-logic-is-wrong/#findComment-496545
Share on other sites

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\" />";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/97029-logic-is-wrong/#findComment-496561
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.