wtfsmd Posted March 31, 2009 Share Posted March 31, 2009 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>'; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151860-need-some-help-with-an-whos-online-plugin/ Share on other sites More sharing options...
Philip Posted March 31, 2009 Share Posted March 31, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/151860-need-some-help-with-an-whos-online-plugin/#findComment-797520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.