Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
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?
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.
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
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.
How do you store the list of users online? Are they in an array, do you just have a string?
Currently I just do
PHP Code:
$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
}
}
// 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.'; }
?>
Last edited by bacterozoid; 09-14-2009 at 07:10 PM..
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:
Quote:
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.
PHP Code:
// 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.'; }
// 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.'; }