...

View Full Version : Resolved Displaying online users



[vengeance]
09-14-2009, 03:33 PM
Hey.

I want to create a simple script, that counts if there's less than/more than 5 online and display it.

If 5 or less:



Name1, name2, name3, name4 and name5 are online.


If more than 5:



Name1, name2, name3, name4, name5 and X more people are online.


The main issue here is that, I don't know how to detect the LAST name, if there's 5 or less users online.

Like, if there's 3 - Name 1, name2 and name3 are online.

Fumigator
09-14-2009, 05:25 PM
Is your question you don't know how to determine, while you're creating the html to display the names, if you are on the last name and therefore display "and" instead of a comma?

[vengeance]
09-14-2009, 05:38 PM
Is your question you don't know how to determine, while you're creating the html to display the names, if you are on the last name and therefore display "and" instead of a comma?

Yeah, that's basically what I'm on a lost track on - simply checking which name is the last and then place "and" in between.

Fumigator
09-14-2009, 05:44 PM
I would just add an "if" statement that checks the current iteration's counter against the number of iterations there are and if they are the same then echo "and" instead of ",". There may be a more clever way of doing it but this works.

bacterozoid
09-14-2009, 05:47 PM
How do you store the list of users online? Are they in an array, do you just have a string?

[vengeance]
09-14-2009, 06:05 PM
How do you store the list of users online? Are they in an array, do you just have a string?

Currently I just do



$getOnline = mysql_query("bla bla get online stuff") or die(mysql_error());
if(mysql_num_rows($getOnline) > 0){
while($online = mysql_fetch_array($getOnline)){
Online users here
}
}

bacterozoid
09-14-2009, 07:03 PM
This should work:


<?php

// Get your list of online users
$getOnline = mysql_query("bla bla get online stuff") or die(mysql_error());

// Constant
$NUMBER_NAMES_TO_SHOW = 5;

// Get the number of rows
$usersOnline = mysql_num_rows($getOnline);

/*
* Loop through 5 times if you have 5+ users online, or less than 5 times if you have < 5
* users online, whichever is smaller.
*/
for($i = 0; $i < min($NUMBER_NAMES_TO_SHOW, $usersOnline); $i++) {

// Fetch the user from your results
$user = mysql_fetch_array($getOnline, MYSQL_ASSOC);

// Print out their name
echo $user['name'];

// Determine which delimiter to print out
if(($i == $usersOnline-2 || $i == $NUMBER_NAMES_TO_SHOW-2) && ($usersOnline <= $NUMBER_NAMES_TO_SHOW)) {
echo ' and ';
} else {
echo ', ';
}

}

// If there are more users online
if($usersOnline > $NUMBER_NAMES_TO_SHOW) {
$otherUsersOnline = $usersOnline - $NUMBER_NAMES_TO_SHOW;
if($otherUsersOnline == 1) {
echo ' and 1 other user is online.';
}
else {
echo ' and ' . $otherUsersOnline . ' other users are online.';
}
}
// If there are no more users online
else {
echo ' are online.';
}

?>

[vengeance]
09-14-2009, 07:38 PM
bacterozoid,

Thanks for the script. However there's a small problem. It displays almost everything correctly - the comma and the "and" works - but it adds a comma after the last username every time.

Example:



Bill, Joe, Ted and Mary, are online.

Bill, Joe, Ted, Mary and Tom, are online.

Bill, Joe, Ted, Mary, Tom, and 2 others are online.




// Get your list of online users
$getOnline = mysql_query("SELECT `id`,`username` FROM `users` WHERE `status` = 'online' ORDER BY `username` ASC") or die(mysql_error());

// Constant
$NUMBER_NAMES_TO_SHOW = 5;

// Get the number of rows
$usersOnline = mysql_num_rows($getOnline);

/*
* Loop through 5 times if you have 5+ users online, or less than 5 times if you have < 5
* users online, whichever is smaller.
*/
for($i = 0; $i < min($NUMBER_NAMES_TO_SHOW, $usersOnline); $i++) {

// Fetch the user from your results
$online = mysql_fetch_array($getOnline, MYSQL_ASSOC);

// Print out their name
echo $online['username'];

// Determine which delimiter to print out
if(($i == $usersOnline-2 || $i == $NUMBER_NAMES_TO_SHOW-2) && ($usersOnline <= $NUMBER_NAMES_TO_SHOW)) {
echo ' and ';
} else {
echo ', ';
}

}

// If there are more users online
if($usersOnline > $NUMBER_NAMES_TO_SHOW) {
$otherUsersOnline = $usersOnline - $NUMBER_NAMES_TO_SHOW;
if($otherUsersOnline == 1) {
echo ' and 1 other user is online.';
}
else {
echo ' and ' . $otherUsersOnline . ' other users are online.';
}
}
// If there are no more users online
else {
echo ' are online.';
}

bacterozoid
09-14-2009, 07:45 PM
Fixed :)


<?php

// Get your list of online users
$getOnline = mysql_query("bla bla get online stuff") or die(mysql_error());

// Constant
$NUMBER_NAMES_TO_SHOW = 5;

// Get the number of rows
$usersOnline = mysql_num_rows($getOnline);

/*
* Loop through 5 times if you have 5+ users online, or less than 5 times if you have < 5
* users online, whichever is smaller.
*/

$iterations = min($NUMBER_NAMES_TO_SHOW, $usersOnline);
for($i = 0; $i < $iterations; $i++) {

// Fetch the user from your results
$user = mysql_fetch_array($getOnline, MYSQL_ASSOC);

// Print out their name
echo $user['name'];

// Determine which delimiter to print out
if(($i == $usersOnline-2 || $i == $NUMBER_NAMES_TO_SHOW-2) && ($usersOnline <= $NUMBER_NAMES_TO_SHOW)) {
echo ' and ';
} else if($i < $iterations-1) {
echo ', ';
}

}

// If there are more users online
if($usersOnline > $NUMBER_NAMES_TO_SHOW) {
$otherUsersOnline = $usersOnline - $NUMBER_NAMES_TO_SHOW;
if($otherUsersOnline == 1) {
echo ' and 1 other user is online.';
}
else {
echo ' and ' . $otherUsersOnline . ' other users are online.';
}
}
// If there are no more users online
else {
echo ' are online.';
}

?>

[vengeance]
09-14-2009, 07:53 PM
Thanks a lot bacterozoid - works flawlessly! Just what I was looking for.

I'm going to take a deeper look into the code and learn from it too. :thumbsup:

bacterozoid
09-14-2009, 07:55 PM
Let me know if you have any questions with it. I tried to comment, but some things may be a bit cryptic.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum