Jump to content

adding , between results


runnerjp

Recommended Posts

<? $result = mysql_query("SELECT * FROM useronline");
while($row = mysql_fetch_array( $result )) {

$last_active = time() - $row['timestamp'];
$onlineuser = $row['user'];
}

if($last_active < 300) {
echo $onlineuser;?>
}
?>

 

the code above works but how would i make it so that its user,user,user  or just user if only one is online

 

 

if $onlineuser== ??

{$onlineuser,}

else

{$onlineuser}

 

Link to comment
https://forums.phpfreaks.com/topic/111559-adding-between-results/
Share on other sites

Just taking a quick stab at this. Basically just enter each user into an array and use the implode() function to seperate the keys with a comma. Good luck :)

 

<? $result = mysql_query("SELECT * FROM useronline");
while($row = mysql_fetch_array( $result )) {

$last_active = time() - $row['timestamp'];
// just turned the variable into an array instead.
$onlineuser[] = $row['user'];
}
// implode the array using a comma to seperate
$implode_users = implode(',',$onlineuser);

if($last_active < 300) {
echo $onlineuser;?>
}
?>

Whoops, I was working on something similiar earlier so I still had that in my noggin. Yeah, you have an array now so you have to get the right key out.

 

This is just a suggestion on how to write a function to do this. My mysql functions are a little rusty as I've been using adodb for several years. Basically what you do is just feed the function the user id (assuming you have that in a session or have it available) and the function will check the database to see if that record exists. If it does it will return the user if the last login time is less than 300. Good luck!

 

<?php
function get_user_online($user) {
  // assumed you're connected to a db already
  //->

  $result = mysql_query("SELECT user_id, last_active FROM usersonline WHERE user_id = $user") or die(mysql_error());
  // check if the query returned any results
  If (mysql_num_rows($result) != 0) {
    $record = mysql_fetch_row($result);
    $last_active = time() - $record[1];
    $user = $record[0];

    // check timestap and return user
    if ($last_active < 300) {
      return $user;
    }
  }
}
?>

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.