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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.