Jump to content

Need some help with an who's online plugin


wtfsmd

Recommended Posts

This works just fine, except for the guest part of it i want it to display something like Guest (3) if there are 3 Guests.

 

What it will do right now is just echo out Guest 3 times.

How could i make it so it will only echo out Guest once and for every Guest after that it will add a number after it inside of the parentheses?

 

<?php
$result = mysql_query("SELECT * FROM online")or die(mysql_error());
while($row = mysql_fetch_array($result)){
$distanceInSeconds = round(abs($row['timeout'] - time()));
        $distanceInMinutes = round($distanceInSeconds / 60);
    if ( $distanceInMinutes <= 15 ) {
    if ( $row['username'] == 'Guest' ) {
	    echo '<li class="link"><a href="#">Guest</a></li>';
	}
    if ( $row['username'] != 'Guest' ) {
		echo '<li class="link"><a href="?page=profile&id=' . $row['id'] . '">' . $row['username'] . '</a></li>';
	}
}
}
?>

Try running your query like:

SELECT `id`, `username`, COUNT(*) as count FROM `online` GROUP BY `username`

<?php
$result = mysql_query("SELECT `id`, `username`, COUNT(*) as count FROM `online` WHERE GROUP BY `username`")or die(mysql_error());
while($row = mysql_fetch_array($result)){
$distanceInSeconds = round(abs($row['timeout'] - time()));
        $distanceInMinutes = round($distanceInSeconds / 60);
    if ( $distanceInMinutes <= 15 ) {
    if ( $row['username'] == 'Guest' ) {
	    echo '<li class="link"><a href="#">Guest ('.$row['count'].')</a></li>';
	}
    if ( $row['username'] != 'Guest' ) {
		echo '<li class="link"><a href="?page=profile&id=' . $row['id'] . '">' . $row['username'] . '</a></li>';
	}
}
}
?>

 

Also, I would consider checking the time in the query instead outside in PHP. You'll get less results and won't have to check them all for times, which result in a faster operation

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.