我正在尝试为我的邮件系统编写通用函数。
我打算做的是。
我从二维关联数组开始,但我无法检索foreach中的值。
最后,foreach echo语句应该像
insert into my_table('name','to_address','subject','message','add_cc') values ('Author1','author1@gmail.com','Subject','Message','test@gmail.com');
insert into my_table('name','to_address','subject','message','add_cc') values ('Author2','author2@gmail.com','Subject','Message','test@gmail.com');
insert into my_table('name','to_address','subject','message','add_cc') values ('Author3','author3@gmail.com','Subject','Message','test@gmail.com');
以下是我的代码,我知道问题在于foreach循环。即使关联数组最小化没有数字序列lik [0],[1],[2],我也会很高兴。
<?php
$to_address = array(
array( 'Name' => "Author1", 'Email' => 'author1@gmail.com' ),
array( 'Name' => "Author2", 'Email' => 'author2@gmail.com' ),
array( 'Name' => "Author3", 'Email' => 'author3@gmail.com')
);
$subject = "Subject";
$message = "Message";
$add_cc = "test@gmail.com";
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);
}
}
}
?>
谢谢, Kimz
编辑:
换句话说,我无法循环$ to_address值,我需要帮助。
答案 0 :(得分:1)
这是你想要的吗?您可以使用单个foreach并将每个数组放在INSERT
。
$to_address = array(
array( 'Name' => "Author1", 'Email' => 'author1@gmail.com' ),
array( 'Name' => "Author2", 'Email' => 'author2@gmail.com' ),
array( 'Name' => "Author3", 'Email' => 'author3@gmail.com')
);
$subject = "Subject";
$message = "Message";
$add_cc = "test@gmail.com";
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
}
}
答案 1 :(得分:1)
您可以在单个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";
}
}
答案 2 :(得分:1)
foreach ( $val as $key1 => $val1 )
$mailReceivers = array(
array( 'Name' => "Author1", 'Email' => 'author1@gmail.com' ),
array( 'Name' => "Author2", 'Email' => 'author2@gmail.com' ),
array( 'Name' => "Author3", 'Email' => 'author3@gmail.com')
);
$subject = "Subject";
$message = "Message";
$add_cc = "test@gmail.com";
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 .')';
}
我调用了函数parseMail,因为我认为你不想打印一个insert语句。称之为你想要的。
你似乎也不知道foreach是如何运作的。玩一下它,看看$key => $val
是什么意思。
我还将foreach从功能中移除。这使它更便于携带,更易于阅读。
答案 3 :(得分:1)
以下是您想要的代码 - 这将创建您需要的Insert查询。
<?php
$to_address = array( array( 'Name' => "Author1", 'Email' => 'author1@gmail.com' ),
array( 'Name' => "Author2", 'Email' => 'author2@gmail.com' ),
array( 'Name' => "Author3", 'Email' => 'author3@gmail.com')
);
$subject = "Subject";
$message = "Message";
$add_cc = "test@gmail.com";
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/>";
}
}
?>
答案 4 :(得分:-1)
这是你想要的吗?
$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);
}
答案 5 :(得分:-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;
}
}