Help... I am trying to build a dynamic associative array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Planet Killer
Forum Newbie
Posts: 3
Joined: Thu May 14, 2009 6:02 pm

Help... I am trying to build a dynamic associative array

Post by Planet Killer »

I am trying to build an a dynamic associative array that is added to each time inside of the loop.
This is so I can send the email and the name to the swift mailer. But it only returns the first one
and I have not found a way to add in other people to the array that will not break the code.

It has to be done dynamically for it to work as it is passed in from a form. I have taken all of the
data that is real and put some fake data in for security purposes.

Code: Select all

 
 
//start loop
foreach ($projmembers as $key => $value) {
 
//echo "key:" . $key . "->" . $value;
               
$membr = mysql_real_escape_string($value);
$x=explode(",",$membr);
                
// Information passed in from the previous form seperated by commas.
//  This information is then split into memberID, membername, and memberemail.
 
    $memberID = $x[0];
    $memberName = $x[1];
    $memberEmail = $x[2];
                
// Here is my main problem.  I am trying to add to a dynamic associative array
// which is going through a loop so I can use these emails as emails to CC out.              
    $ccemails = array($memberEmail => $memberName);
              
//$insert = mysql_query("INSERT INTO ec_project_members VALUES ('','$projectID','$memberID')") or die("There was an error while inserting database data : " . mysql_error());
}
//end loop
            
//Set msgbody
    $msgbody = "<center><h3>bodyofemail " . $projname . "</h3><p><b>Project Objectives: </b>";
    $subject = "SaberNet Employee Contribution - " . $projname;
            
//Send the trasport to send the email(s)
    $transport = Swift_SmtpTransport::newInstance('smtp.server.com', 25)
    ->setUsername('blah@blah.com')
    ->setPassword('password');
 
//Create the Mailer using your created Transport
  $mailer = Swift_Mailer::newInstance($transport);
            
//Prep the email(s)
  $message = Swift_Message::newInstance($subject)
  ->setFrom(array('blah@censoredemail.com=> 'blimey'))
  ->setTo(array('blah@censoredemail.com' => 'blah'))
  ->setCc($ccemails)
  ->addpart($msgbody,'text/html');
            
//Send the email(s)
   $mailresult = $mailer->send($message);
}
 
 
Last edited by Benjamin on Thu May 14, 2009 7:43 pm, edited 1 time in total.
Reason: Changed code type from text to php.
nyoka
Forum Commoner
Posts: 45
Joined: Thu Apr 09, 2009 12:53 pm

Re: Help... I am trying to build a dynamic associative array

Post by nyoka »

Have you tried something like (not the change on line 20):

Code: Select all

 
 
//start loop
foreach ($projmembers as $key => $value) {
 
//echo "key:" . $key . "->" . $value;
              
$membr = mysql_real_escape_string($value);
$x=explode(",",$membr);
               
// Information passed in from the previous form seperated by commas.
//  This information is then split into memberID, membername, and memberemail.
 
    $memberID = $x[0];
    $memberName = $x[1];
    $memberEmail = $x[2];
               
// Here is my main problem.  I am trying to add to a dynamic associative array
// which is going through a loop so I can use these emails as emails to CC out.             
    $ccemails[] = array($memberEmail => $memberName);
             
//$insert = mysql_query("INSERT INTO ec_project_members VALUES ('','$projectID','$memberID')") or die("There was an error while inserting database data : " . mysql_error());
}
//end loop
           
//Set msgbody
    $msgbody = "<center><h3>bodyofemail " . $projname . "</h3><p><b>Project Objectives: </b>";
    $subject = "SaberNet Employee Contribution - " . $projname;
           
//Send the trasport to send the email(s)
    $transport = Swift_SmtpTransport::newInstance('smtp.server.com', 25)
    ->setUsername('blah@blah.com')
    ->setPassword('password');
 
//Create the Mailer using your created Transport
  $mailer = Swift_Mailer::newInstance($transport);
           
//Prep the email(s)
  $message = Swift_Message::newInstance($subject)
  ->setFrom(array('blah@censoredemail.com=> 'blimey'))
  ->setTo(array('blah@censoredemail.com' => 'blah'))
  ->setCc($ccemails)
  ->addpart($msgbody,'text/html');
           
//Send the email(s)
   $mailresult = $mailer->send($message);
}
 
 
 
Planet Killer
Forum Newbie
Posts: 3
Joined: Thu May 14, 2009 6:02 pm

Re: Help... I am trying to build a dynamic associative array

Post by Planet Killer »

Well I get this error when I try it:

Warning: preg_match() expects parameter 2 to be string, array given in C:\Program Files\VertrigoServ\www\lib\classes\Swift\Mime\Headers\MailboxHeader.php on line 317

Code: Select all

 
[color=#BF00FF]$ccemails[] = array($memberEmail => $memberName);[/color]
 
$message = Swift_Message::newInstance($subject)
      ->setFrom(array('blah@blah.org' => 'blah.org'))
      ->setTo(array('blah@blah.org'' => 'blah'))
    [color=#FF0080]  ->setCc($ccemails)[/color]
      ->addpart($msgbody,'text/html');
    
//Send the email(s)
        $mailresult = $mailer->send($message);
 
 
nyoka
Forum Commoner
Posts: 45
Joined: Thu Apr 09, 2009 12:53 pm

Re: Help... I am trying to build a dynamic associative array

Post by nyoka »

You need to make sure you know what data is expected by the follow line:
<code>
->setCc($ccemails)
</code>

At least you have all the email addresses in an array, you can manipulate it as necessary once you understand the above line.
Post Reply