0

I'm trying to write a generic function for my mailing system.

What I'm planning to do is.

  1. Get all the to-address values in an array and the required information
  2. Store all the values in the database.

I started with two dimensional associate array, but I'm not able to retrieve the values in the foreach.

Finally the foreach echo statement should be like

insert into my_table('name','to_address','subject','message','add_cc') values ('Author1','[email protected]','Subject','Message','[email protected]');
insert into my_table('name','to_address','subject','message','add_cc') values ('Author2','[email protected]','Subject','Message','[email protected]');
insert into my_table('name','to_address','subject','message','add_cc') values ('Author3','[email protected]','Subject','Message','[email protected]');

Here below is my code, I know the problem is with the foreach loop. I would be happy even if the associate array is minimized without the numeric sequence lik [0],[1],[2].

<?php
$to_address = array(
    array( 'Name' => "Author1", 'Email' => '[email protected]' ),
    array( 'Name' => "Author2",  'Email' => '[email protected]' ),
    array( 'Name' => "Author3", 'Email' => '[email protected]')
);

$subject = "Subject";
$message = "Message";
$add_cc  = "[email protected]";
sendMail($to_address,$subject,$message,$add_cc);

function sendMail($to_address,$subject,$message,$add_cc) {
    foreach ($to_address as $key => $val) {
        foreach ($val as $key1 => $val1) {
            print_r($val1);
        }
    }
}

?>

Thanks, Kimz

EDIT:

In other words, I'm not able to loop the $to_address value and I need a help.

0

6 Answers 6

1

Here is the code you want - This will create the Insert queries you require.

<?php
$to_address = array( array( 'Name' => "Author1", 'Email' => '[email protected]' ),
array( 'Name' => "Author2",  'Email' => '[email protected]' ),
array( 'Name' => "Author3", 'Email' => '[email protected]')
);
$subject = "Subject";
$message = "Message";
$add_cc  = "[email protected]";
sendMail($to_address,$subject,$message,$add_cc);

function sendMail($to_address,$subject,$message,$add_cc){

    foreach ($to_address as $key => $val) {
        $sqlStatement =  "INSERT INTO my_table('name','to_address','subject','message','add_cc') VALUES ('".$val['Name']."','".$val['Email']."','".$subject."','".$message ."','".$add_cc."');";
        echo "$sqlStatement <br/>";
    }
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

You can retrieve the values in a single foreach:

function sendMail($to_address,$subject,$message,$add_cc){

    foreach ($to_address as $val) {
       $name = $val["Name"];
       $email = $val["Email"];
       echo "Name: $name, Email: $email <br\>\n";
    }
}

Comments

1

Remarks first

  • Use names that tell me what is inside a variable. thinngs like $val => $key tell me nothing. neither do foreach ( $val as $key1 => $val1 )
  • indentation: use it, and look at psr-*
  • again, describe what is happening. a function sendMail should send an email. Not print a query

The solution

$mailReceivers = array(
    array( 'Name' => "Author1", 'Email' => '[email protected]' ),
    array( 'Name' => "Author2",  'Email' => '[email protected]' ),
    array( 'Name' => "Author3", 'Email' => '[email protected]')
);

$subject = "Subject";
$message = "Message";
$add_cc  = "[email protected]";

foreach ( $mailReceivers as $receiver )
{
    parseMail($reciever['Name'], $reciever['Email'], $subject, $message, $add_cc);
}

function parseMail($name, $email, $subject, $message, $cc)
{
    print 'INSERT INTO my_table ('name','to_address','subject','message','add_cc') VALUES ('. $name .', '. $email .', '. $subject .', '. $message .', '. $subject .')';
}

Remarks

I called the function parseMail since I do'nt think you want to print an insert statement. Call it what you want it to be.

You also seem to not know how foreach works. Play a little with it and see what the $key => $val thing meens.

I also moved the foreach out of the function.this makes it more portable and is easier to read.

Comments

1

This is how you want it? You can just use a single foreach and put each array in the INSERT.

    $to_address = array(
        array( 'Name' => "Author1", 'Email' => '[email protected]' ),
        array( 'Name' => "Author2",  'Email' => '[email protected]' ),
        array( 'Name' => "Author3", 'Email' => '[email protected]')
    );

    $subject = "Subject";
    $message = "Message";
    $add_cc  = "[email protected]";
    sendMail($to_address,$subject,$message,$add_cc);

function sendMail($to_address,$subject,$message,$add_cc) {
          foreach ($to_address as $val) {
              $sql = "INSERT INTO my_table('name','to_address','subject','message','add_cc') 
                      VALUES ('".$val['Name']."','".$val['Email']."','".$subject."','".$message ."','".$add_cc."');";
              echo $sql . "<br>";
              //Use your Insert Statement
          }
}

Comments

-1
   function sendMail($to_address,$subject,$message,$add_cc){

            foreach ($to_address as $key => $val) {

               extract($val);
               // use the keys of $val array as variable names 
               // http://php.net/manual/en/function.extract.php

               // sql for data base query
               $sql = "INSERT INTO my_table('name','to_address','subject','message','add_cc') VALUES ('".$Name."','".$Email."','".$subject."','".$message ."','".$add_cc."');";

               echo $sql;
            }
        }

2 Comments

whats wrong with a simple array access? you only need the name and to_addreS. all you know the array could have tons of keys
nothing , i just like to use extract as the code becomes more readable and the variables becomes self-explanatory. And yes it would become tedious to use with array with tons of keys
-1

Is this what you want?

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

foreach ($to_address as $val) {

$sql = "insert into my_table('name','to_address','subject','message','add_cc') values (?,?,?,?,?)";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, $val['Name'], $val['Email'], $subject, $message, $add_cc);
);

mysqli_query($link, $sql);
}

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.