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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.