Page 1 of 1

Whos online problem!

Posted: Wed Jun 09, 2004 4:35 pm
by Joe
I have been trying to create a whos online script where the administrators are shown as blue and the normal users are shown as green. Admins have an ID of 1 while normal users have an ID of 0 and when I stitched the code together I was getting no luck out of it at all. My code go's like:

Code: Select all

$query = "SELECT * FROM users WHERE username = '".$_SESSION['username']."'";
$result = mysql_query($query) or die("Error!");
$row = mysql_fetch_assoc($result);
$ID1 = $row['ID'];

$sql = "SELECT * FROM online WHERE level = '1'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

while (true)
{
 $row = mysql_fetch_assoc($result);
 if ($row == false) break;
 $useronline = $row['user'];

 echo "<A href='http://www.site.com/viewprofile.php?ID=$ID1'><font color='blue'>$useronline</A>,";
}

$sql = "SELECT * FROM online WHERE level = '0'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

while (true)
{
 $row = mysql_fetch_assoc($result);
 if ($row == false) break;
 $useronline = $row['user'];

 echo "<A href='http://www.site.com/viewprofile.php?ID=$ID1'><font color='green'>$useronline</A>,";
}
When I tested the code the results showed nothing for some reason I do not know. Can anyone help out here, I know the code is gritty but im just trying to get something up!


All the best



Joe 8)

Posted: Wed Jun 09, 2004 4:44 pm
by feyd
what's the table structure of `online`?

Posted: Wed Jun 09, 2004 4:48 pm
by Joe
Here is the main structure...


level - int

user - text

userpro - text

Active - int

ip - varchar

time int

Posted: Wed Jun 09, 2004 5:00 pm
by feyd
try replacing all your code posted with this:

Code: Select all

<?php

$sql = "SELECT o.user, o.level, u.ID FROM online o, users u ORDER BY o.level DESC, o.user ASC"; 
$result = mysql_query($sql); 
$first = true;
while ($row = mysql_fetch_assoc($result))
{
	if(!$first)
		echo ', ';
	echo '<a href="http://www.site.com/viewprofile.php?ID='.$row['ID'].'"><font color="'.(($row['level'] == '1')?'blue':'green').'">'.$row['user'].'</a>';
	$first = false;
} 

?>
This should order all admins first, followed by all users, each being in alphabetic order.