Maniacility Posted July 29, 2009 Share Posted July 29, 2009 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! Link to comment https://forums.phpfreaks.com/topic/167965-solved-members-online/ Share on other sites More sharing options...
bruce080 Posted July 29, 2009 Share Posted July 29, 2009 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++; } ?> Link to comment https://forums.phpfreaks.com/topic/167965-solved-members-online/#findComment-885944 Share on other sites More sharing options...
abazoskib Posted July 29, 2009 Share Posted July 29, 2009 or end() but im not sure if it works with a while loop. i know for sure it works in a foreach loop and returns TRUE if the item is the last in the array Link to comment https://forums.phpfreaks.com/topic/167965-solved-members-online/#findComment-885956 Share on other sites More sharing options...
Maniacility Posted July 29, 2009 Author Share Posted July 29, 2009 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. Link to comment https://forums.phpfreaks.com/topic/167965-solved-members-online/#findComment-885971 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.