Whos online problem!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Whos online problem!

Post 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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's the table structure of `online`?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Here is the main structure...


level - int

user - text

userpro - text

Active - int

ip - varchar

time int
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply