madness69 Posted October 18, 2012 Share Posted October 18, 2012 hello there, im trying to remove the last comma in my code, how coould i? $query = "SELECT email FROM users WHERE level = '2'"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "{$row['email']}, " ; } Best regards Link to comment https://forums.phpfreaks.com/topic/269644-removing-comma/ Share on other sites More sharing options...
TOA Posted October 18, 2012 Share Posted October 18, 2012 You can't since you're echoing your content right away. Try capturing your output first like this: $query = "SELECT email FROM users WHERE level = '2'"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // capture it so we can remove extra comma later $html .= "{$row['email']}, "; } // since it's on an end, we can use a form of trim $html = rtrim($html, ', '); // now echo it echo $html; Link to comment https://forums.phpfreaks.com/topic/269644-removing-comma/#findComment-1386113 Share on other sites More sharing options...
fesan Posted October 18, 2012 Share Posted October 18, 2012 I'm no expert but cant you just do it like this: $query = "SELECT email FROM users WHERE level = '2'"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo $row['email']; } Link to comment https://forums.phpfreaks.com/topic/269644-removing-comma/#findComment-1386124 Share on other sites More sharing options...
TOA Posted October 18, 2012 Share Posted October 18, 2012 He wanted comma separated, which added one to the end. What you posted would just keep adding them on one line with no breaks or spaces. But, you made me rethink me previous post and @OP, there's an alternative (IMO better) way: $query = "SELECT email FROM users WHERE level = '2'"; $result = mysql_query($query); $emails = array(); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // add each one to the array $emails[] = "{$row['email']}"; } // now we implode() the array adding commas, which will avoid our last one for us $html = implode($emails, ','); // now echo it echo $html; Link to comment https://forums.phpfreaks.com/topic/269644-removing-comma/#findComment-1386126 Share on other sites More sharing options...
fesan Posted October 18, 2012 Share Posted October 18, 2012 Yea... Sorry! Did not get that at first... Link to comment https://forums.phpfreaks.com/topic/269644-removing-comma/#findComment-1386128 Share on other sites More sharing options...
Christian F. Posted October 18, 2012 Share Posted October 18, 2012 TOA: Why this? $emails[] = "{$row['email']}" Completely unnecessary use of quotes and the advanced string syntax, I'd do away with both: $emails[] = $row['email']; Link to comment https://forums.phpfreaks.com/topic/269644-removing-comma/#findComment-1386145 Share on other sites More sharing options...
TOA Posted October 18, 2012 Share Posted October 18, 2012 @christian - because that's how he had it and I didn't re-write the entire thing, just made some small changes to show usage. But, good point and worth noting OP Link to comment https://forums.phpfreaks.com/topic/269644-removing-comma/#findComment-1386151 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.