0

I have this PHP Code:

while($contacts2=mysql_fetch_array($rs))
    {
        //generate the list of emails address in an array
        $emails_list[] = $contacts2["email"];
    }

so it puts all my results into an array but when i echo $emails_list outside of the while loop it just displays Array

how can i make it display like:

result1,result2,result3 etc

3
  • 6
    RTFM: php.net/manual/en/function.implode.php Commented Oct 7, 2013 at 10:03
  • 1
    echo implode(',',$emails_list); Commented Oct 7, 2013 at 10:03
  • implode is definitely the way forward here. Commented Oct 7, 2013 at 10:07

6 Answers 6

1

Here is your answer

while($contacts2=mysql_fetch_array($rs))
{
     //generate the list of emails address in an array
     $emails_list[] = $contacts2["email"];
}

$emails_list = implode(',', $emails_list);
echo "(". $emails_list . ")";

I think you should try like this,

$mail->AddAddress('[email protected]', 'Person One');
$mail->AddAddress('[email protected]', 'Person Two');

With PHPMailer, you can do as,

while($contacts2=mysql_fetch_array($rs))
{
    $mail->AddAddress($contacts2['emails'], $contacts2['name']);
}
Sign up to request clarification or add additional context in comments.

Comments

1

$emails_list is an array, so you have to loop through it to print its values:

foreach ($emails_list as $email) {
  print "email: $email";
}

Note that if you want to print an specific value, you can address with $emails_list[index]. So you can do print $emails_list[0], for example.

If you then want to print values all together, do the following:

echo "(" . implode(',',$emails_list) . ")";

Test

$a=array(1,2,3);
echo "(" . implode(',',$a) . ")";

Returns

(1,2,3)

Update

Your code in http://pastebin.com/BwWZFrzZ shows that you are using:

echo 'Email sent to:' . $emails_list . '<br/ >';

So you can print

echo 'Email sent to: (' . implode(',',$emails_list) . ')<br/ >';

Update 2

Based on the github code it comes from (https://github.com/PHPMailer/PHPMailer), you need to add one email at a time:

//this is some clever **** to do with sending emails with attachments :D
$email = new PHPMailer();

while($contacts2=mysql_fetch_array($rs))
   $email->AddAddress($contacts2["email"]);
}

$email->From      = '...';

11 Comments

i am trying to run this code : pastebin.com/BwWZFrzZ : which sends emails using github.com/PHPMailer/PHPMailer
but i am getting an error saying: Mailer Error: You must provide at least one recipient email address. when i use $emails_list = implode(',',$emails_list); and echo $emails_list i get a list of all the email addresses separated by a comma
even if i use $email->AddAddress(implode(',',$emails_list)); i still get Mailer Error: You must provide at least one recipient email address.
You should check how AddAddress wants the list of emails to be given. Does it need an array or a list?
im not sure - it works fine if i use just a single email address though - like: $email->AddAddress("[email protected]")
|
0

try this

echo implode(',', $emails_list);

here you will find the full details php implode

Comments

0

The implode() function returns a string from the elements of an array.

 echo implode(",",$emails_list);

1 Comment

The implode() function returns a string from the elements of an array.
0
array_push($emails_list,$contacts2["email"]);

then print the $emails_list outside of loop may be solve your problem

Comments

0

Try this code

while($contacts2=mysql_fetch_array($rs))
    {
        //generate the list of emails address in an array
        array_push($emails_list,$contacts2["email"]);
    }

To show array data you can check with a print_r (just checking if it works)

print_r($emails_list)

and you can create a loop to take data one by one like below

for(int i=0;i<count($emails_list);$i++)
{
  echo $emails_list[$i]."<br>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.