Jump to content

[SOLVED] rtrim function


Da Foxx

Recommended Posts

$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

<?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

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>, ";
}
?>
?>

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. :)

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 = ', ';
}

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!  :D

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.