Mr Nick Posted July 12, 2010 Share Posted July 12, 2010 So far my simple code works properly. It outputs: "user1, user2, user3, " However I'd like it to output: "user1, user2 and user3." while($user_fetch = $db->fetch_assoc($chars_fetch)){ echo $user_fetch['username'] . ', '; } Link to comment https://forums.phpfreaks.com/topic/207538-comma-problem/ Share on other sites More sharing options...
Psycho Posted July 12, 2010 Share Posted July 12, 2010 $userList = array(); while($user_fetch = $db->fetch_assoc($chars_fetch)) { $userList[] = $user_fetch['username']; } echo implode(',', $userList); Link to comment https://forums.phpfreaks.com/topic/207538-comma-problem/#findComment-1085068 Share on other sites More sharing options...
Mr Nick Posted July 12, 2010 Author Share Posted July 12, 2010 That's not exactly what I want to do . It's already outputting to "user1, user2, user3, " so the commas are already displaying. Just for the last part ("user3, "), I'd want the command removed and replaced with a period. EDIT: The actual variable ( $user_fetch['username'] ) is the actual usernames. There is no comma in this. Link to comment https://forums.phpfreaks.com/topic/207538-comma-problem/#findComment-1085069 Share on other sites More sharing options...
AbraCadaver Posted July 12, 2010 Share Posted July 12, 2010 That's not exactly what I want to do . It's already outputting to "user1, user2, user3, " so the commas are already displaying. Just for the last part ("user3, "), I'd want the command removed and replaced with a period. EDIT: The actual variable ( $user_fetch['username'] ) is the actual usernames. There is no comma in this. The way mjdamato has shown is the fairly standard way of doing this. To add a period: echo implode(',', $userList) . '.'; It's going to be a big hassle echoing each as you have shown. Link to comment https://forums.phpfreaks.com/topic/207538-comma-problem/#findComment-1085079 Share on other sites More sharing options...
Mr Nick Posted July 12, 2010 Author Share Posted July 12, 2010 Awesome! Thanks man. Link to comment https://forums.phpfreaks.com/topic/207538-comma-problem/#findComment-1085081 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.