Jump to content

[SOLVED] Members Online


Maniacility

Recommended Posts

Hey, I have this piece of code right and what it does is display all my members that are online.

 

<?php

$select = mysql_query("SELECT * FROM `users` WHERE `online` > '".$timenow."' ORDER by `username`");

while ($info = mysql_fetch_object($select)){

echo "<a href='profile.php?user=$info->id'>$info->username</a>, ";

} 

?>

 

What this will currently do is display them like this:

 

FirstUser, SecondUser, ThirdUser, LastUser,

 

Now, the problem is on the last user I always have that comma (,) because i'm doing a while loop where each result has one, but is there any way that on the last user the loop ends it differently? e.g. nothing there or maybe a fullstop like this:

 

FirstUser, SecondUser, ThirdUser, LastUser.

 

or

 

FirstUser, SecondUser, ThirdUser, LastUser

 

Can anyone help?

Thanks! :D

Link to comment
https://forums.phpfreaks.com/topic/167965-solved-members-online/
Share on other sites

Sure, there is a way to do it, but it would be easier to do it the other way.  If it is the first user, don't add a comma before it, and if it is not the first user, add a comma before it.

 

<?php

$select = mysql_query("SELECT * FROM `users` WHERE `online` > '".$timenow."' ORDER by `username`");

$counter=0;
while ($info = mysql_fetch_object($select)){
     if ($counter==0) {
          echo "<a href='profile.php?user=$info->id'>$info->username</a>";
     }
     else {
          echo ", <a href='profile.php?user=$info->id'>$info->username</a>";
     }
     $counter++;
} 

?>

Sure, there is a way to do it, but it would be easier to do it the other way.  If it is the first user, don't add a comma before it, and if it is not the first user, add a comma before it.

 

<?php

$select = mysql_query("SELECT * FROM `users` WHERE `online` > '".$timenow."' ORDER by `username`");

$counter=0;
while ($info = mysql_fetch_object($select)){
     if ($counter==0) {
          echo "<a href='profile.php?user=$info->id'>$info->username</a>";
     }
     else {
          echo ", <a href='profile.php?user=$info->id'>$info->username</a>";
     }
     $counter++;
} 

?>

 

Worked perfectly, thanks so much.

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.