Da Foxx Posted July 28, 2009 Share Posted July 28, 2009 $users_online = User::find_by_sql("SELECT * FROM members WHERE UNIX_TIMESTAMP(last_action) >= '".(time()-600)."' ORDER BY last_action DESC"); echo "There are <b>".count($users_online)."</b> user(s) online within the last 10 minutes:<br />"; foreach($users_online as $user) { echo "<a href=\"index.php?profile=$user->username\">$user->username</a>, "; } That code works fine, but my question is how can I make it so that the last username to be displayed would have the "," stripped from it? I know about rtrim, but I had no luck with it. Link to comment https://forums.phpfreaks.com/topic/167861-solved-rtrim-function/ Share on other sites More sharing options...
WolfRage Posted July 28, 2009 Share Posted July 28, 2009 <?php $user->username=rtrim($user->username,','); ?> Link to comment https://forums.phpfreaks.com/topic/167861-solved-rtrim-function/#findComment-885340 Share on other sites More sharing options...
Da Foxx Posted July 28, 2009 Author Share Posted July 28, 2009 <?php $user->username=rtrim($user->username,','); ?> I tried that, but I can't seem to make it work. How would I add that to the foreach loop, if I can? Link to comment https://forums.phpfreaks.com/topic/167861-solved-rtrim-function/#findComment-885345 Share on other sites More sharing options...
WolfRage Posted July 28, 2009 Share Posted July 28, 2009 <?php foreach($users_online as $user) { $user->username=rtrim($user->username,','); echo "<a href=\"index.php?profile=$user->username\">$user->username</a>, "; } ?> Link to comment https://forums.phpfreaks.com/topic/167861-solved-rtrim-function/#findComment-885349 Share on other sites More sharing options...
Da Foxx Posted July 28, 2009 Author Share Posted July 28, 2009 <?php foreach($users_online as $user) { $user->username=rtrim($user->username,','); echo "<a href=\"index.php?profile=$user->username\">$user->username</a>, "; } ?> Does not work, even if I place "," like this : <a href=\"index.php?profile=$user->username\">$user->username,</a> I want to create this type of effect: Username, Username, Username, Username, Username Link to comment https://forums.phpfreaks.com/topic/167861-solved-rtrim-function/#findComment-885358 Share on other sites More sharing options...
WolfRage Posted July 28, 2009 Share Posted July 28, 2009 Ok Now I understand you better. <?php <?php $total=count($users_online); foreach($users_online as $user) { $count++; if($total===$count) { $user->username=rtrim($user->username,','); } echo "<a href=\"index.php?profile=$user->username\">$user->username</a>, "; } ?> ?> Link to comment https://forums.phpfreaks.com/topic/167861-solved-rtrim-function/#findComment-885370 Share on other sites More sharing options...
Da Foxx Posted July 28, 2009 Author Share Posted July 28, 2009 Ok Now I understand you better. <?php <?php $total=count($users_online); foreach($users_online as $user) { $count++; if($total===$count) { $user->username=rtrim($user->username,','); } echo "<a href=\"index.php?profile=$user->username\">$user->username</a>, "; } ?> ?> Thanks, but I figured out the solution to it just a few second ago. I used this code instead: <?php $users_online = User::find_by_sql("SELECT * FROM members WHERE UNIX_TIMESTAMP(last_action) >= '".(time()-600)."' ORDER BY last_action DESC"); $online = "There are <b>".count($users_online)."</b> user(s) online within the last 10 minutes:<br />"; foreach($users_online as $user) { $online .= "<a href=\"index.php?profile=$user->username\">$user->username</a>" . ", "; } $online = rtrim($online, ", "); $online .= "</td></tr></table>"; echo $online; ?> I'll try out your code because it looks less messy. Link to comment https://forums.phpfreaks.com/topic/167861-solved-rtrim-function/#findComment-885373 Share on other sites More sharing options...
sasa Posted July 28, 2009 Share Posted July 28, 2009 or $users_online = User::find_by_sql("SELECT * FROM members WHERE UNIX_TIMESTAMP(last_action) >= '".(time()-600)."' ORDER BY last_action DESC"); echo "There are <b>".count($users_online)."</b> user(s) online within the last 10 minutes:<br />"; $comma = ''; foreach($users_online as $user) { echo $comma, "<a href=\"index.php?profile=$user->username\">$user->username</a>"; $comma = ', '; } Link to comment https://forums.phpfreaks.com/topic/167861-solved-rtrim-function/#findComment-885376 Share on other sites More sharing options...
Da Foxx Posted July 28, 2009 Author Share Posted July 28, 2009 or $users_online = User::find_by_sql("SELECT * FROM members WHERE UNIX_TIMESTAMP(last_action) >= '".(time()-600)."' ORDER BY last_action DESC"); echo "There are <b>".count($users_online)."</b> user(s) online within the last 10 minutes:<br />"; $comma = ''; foreach($users_online as $user) { echo $comma, "<a href=\"index.php?profile=$user->username\">$user->username</a>"; $comma = ', '; } That is awesome! That works great! Link to comment https://forums.phpfreaks.com/topic/167861-solved-rtrim-function/#findComment-885377 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.