Jump to content

Removing Comma


madness69

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.