March 18, 2012

php array and mysql syntax error

Question by Andras Sebestyen

I am trying SwiftMailer to fetch the email addresses to an array() I have the original example and I know what I want to put in it but I can’t see the forest from the trees… any one out there with proper eyes please:

// Send the message
$result = $mailer->send($message);

// Send the message
$failedRecipients = array();
$numSent = 0;

Here is where I am struggling:

 foreach($groups as $value)
    { 
             echo "<h3>".$value."</h3>"; //this is working

    // get the email groups
    $sql="SELECT * FROM emails WHERE sendesstat=1 AND deleted=0 AND classhoz="".$value.""";
    $query=mysql_query($sql);

    // fetch the emails from the group
    while($data=mysql_fetch_array($query))
                {
                  $emil="'".$data["email"]."' => '".$data["firstname"]." ".$data["surname"]."'";
                  echo $emil;

this echo is working but I can’t put in the $to[] array… (sorry) and this is how it should appear:

$to = array('receiver@domain.org', 'other@domain.org' => 'A name');

the issue is the given name => 'A name' I am being sick and I can’t find the syntax !!!

                  $to[ ]= $emil;            
                }
    }

foreach ($to as $address => $name)
{
  if (is_int($address)) {
   $message->setTo($name);
  } else {
   $message->setTo(array($address => $name));
}

 $numSent += $mailer->send($message, $failedRecipients);
}

printf("Sent %d messagesn", $numSent);

please please everything else is working now only this bit is missing. Thank you

Answer by Starx

You are defining the $to array the wrong way.

Use this to add an item on the array

$to[$data["email"]] = $data["firstname"]." ".$data["surname"];
//  ^ Index           ^ Value

Later On, when you use foreach loop it will well differentiated into $address and $name, just the way you are doing.

foreach ($to as $address => $name) {
    echo $address;
    echo $name; 
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!