英文:
PHP - text not added to a variable inside foreach loop
问题
我有这段代码在我的WordPress插件中,它应该从数据库中获取一些数据,然后发送电子邮件到不同的地址,主题相同但电子邮件正文不同。
<?php
$pdv_subject = "Confirmation links from" . date('d-m-Y', time());
//
$pdv_message_a = "Salut!\n";
$pdv_email_a = 'user@example.com';
$pdv_headers_a[] = 'Cc: user@example.com';
//
$pdv_message_b = "Ciao!\n";
$pdv_email_b = 'user@example.com';
$pdv_headers_b[] = 'Cc: user@example.com';
//
$pdv_message_p = "Hello!\n";
$pdv_email_p = 'user@example.com';
$pdv_headers_p[] = 'Cc: user@example.com';
//
foreach( $results as $key => $val ){
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['pco'] === '650'){
$pdv_message_a .= $val['link'] . "\n";
}
//
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['pco'] === '620'){
$pdv_message_b .= $val['link'] . "\n";
}
//
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['pco'] === '660' ){
$pdv_message_p .= $val['link'] . "\n";
}
}
在代码中,我省略了wp_mail
函数,我已经进行了测试,它运行正常。我唯一的问题是$pdv_message_
需要添加到if语句内部,否则消息将不会被添加,这将导致电子邮件发送时没有正文内的链接。我已经使用var_dump()
查看了$val
,但为什么链接没有添加到消息中呢?
英文:
I have this code in my WordPress plugin, it's supposed to get some data from database and after that send emails to different adresses with a common subject but different email body.
<?php
$pdv_subject = "Confirmation links from" . date('d-m-Y', time());
//
$pdv_message_a = "Salut!\n";
$pdv_email_a = 'user@example.com';
$pdv_headers_a[] = 'Cc: user@example.com';
//
$pdv_message_b = "Ciao!\n";
$pdv_email_b = 'user@example.com';
$pdv_headers_b[] = 'Cc: user@example.com';
//
$pdv_message_p = "Hello!\n";
$pdv_email_p = 'user@example.com';
$pdv_headers_p[] = 'Cc: user@example.com';
//
foreach( $results as $key => $val ){
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['pco'] === '650'){
$pdv_message_a .= $val['link'] . "\n";
}
//
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['pco'] === '620'){
$pdv_message_b .= $val['link'] . "\n";
}
//
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['pco'] === '660' ){
$pdv_message_p .= $val['link'] . "\n";
}
}
In the code I've omitted the wp_mail
function, I've done a test and it's working fine. The only problem I have is that the $pdv_message_
that needs to be added inside the if statement will be not added, this will cause that the email will be sent without the links inside the body. I've done a var_dump()
and I'm able to see the $val
but why the links aren't added to the messages?
答案1
得分: 3
除此之外,我认为我会将代码布局如下:
foreach( $results as $key => $val ){
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time())) {
switch ($val['pco']) {
case '620':
$pdv_message_b .= $val['link'] . "\n";
break;
case '650':
$pdv_message_a .= $val['link'] . "\n";
break;
case '660':
$pdv_message_p .= $val['link'] . "\n";
break;
}
}
}
(我并不是在暗示这是解决您的问题的答案,但在我看来,它看起来更漂亮,避免了重复的if条件。)
英文:
Aside from anything, I think I'd lay the code out like this
foreach( $results as $key => $val ){
if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time())) {
switch ($val['pco']) {
case '620':
$pdv_message_b .= $val['link'] . "\n";
break;
case '650':
$pdv_message_a .= $val['link'] . "\n";
break;
case '660':
$pdv_message_p .= $val['link'] . "\n";
break;
}
}
}
(I'm not suggesting this is an answer to your problem, but it looks a lot nicer IMO and saves repeating all those identical if clauses.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论