Jump to content

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

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.