runnerjp Posted June 23, 2008 Share Posted June 23, 2008 <? $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 More sharing options...
lonewolf217 Posted June 23, 2008 Share Posted June 23, 2008 in pseudo code, you could get the count of how many results you have, then use a reverse for loop for x = count; x>0; x++ output user so you know exactly when that last user is coming so you can arrange when to show commas Link to comment https://forums.phpfreaks.com/topic/111559-adding-between-results/#findComment-572591 Share on other sites More sharing options...
gijew Posted June 23, 2008 Share Posted June 23, 2008 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;?> } ?> Link to comment https://forums.phpfreaks.com/topic/111559-adding-between-results/#findComment-572594 Share on other sites More sharing options...
runnerjp Posted June 23, 2008 Author Share Posted June 23, 2008 humm ok that outputted "array" for some reason Link to comment https://forums.phpfreaks.com/topic/111559-adding-between-results/#findComment-572595 Share on other sites More sharing options...
gijew Posted June 23, 2008 Share Posted June 23, 2008 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; } } } ?> Link to comment https://forums.phpfreaks.com/topic/111559-adding-between-results/#findComment-572604 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.