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
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;

Edited by TOA
Link to comment
Share on other sites

@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

Edited by TOA
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.