PHP – 在foreach循环内未将文本添加到变量中

huangapple go评论49阅读模式
英文:

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.

&lt;?php

$pdv_subject = &quot;Confirmation links from&quot; . date(&#39;d-m-Y&#39;, time());
//
$pdv_message_a = &quot;Salut!\n&quot;;
$pdv_email_a = &#39;user@example.com&#39;;
$pdv_headers_a[] = &#39;Cc: user@example.com&#39;;

//
$pdv_message_b = &quot;Ciao!\n&quot;;
$pdv_email_b = &#39;user@example.com&#39;;
$pdv_headers_b[] = &#39;Cc: user@example.com&#39;;


//
$pdv_message_p = &quot;Hello!\n&quot;;
$pdv_email_p = &#39;user@example.com&#39;;
$pdv_headers_p[] = &#39;Cc: user@example.com&#39;;


//
foreach( $results as $key =&gt; $val ){
    if(date(&#39;d-m-Y&#39;, $val[&#39;confirmed_at&#39;]) === date(&#39;d-m-Y&#39;, time()) &amp;&amp; $val[&#39;pco&#39;] === &#39;650&#39;){
        $pdv_message_a .= $val[&#39;link&#39;] . &quot;\n&quot;;
    } 
    //
    if(date(&#39;d-m-Y&#39;, $val[&#39;confirmed_at&#39;]) === date(&#39;d-m-Y&#39;, time()) &amp;&amp; $val[&#39;pco&#39;] === &#39;620&#39;){
        $pdv_message_b .= $val[&#39;link&#39;] . &quot;\n&quot;;
    }
    // 
    if(date(&#39;d-m-Y&#39;, $val[&#39;confirmed_at&#39;]) === date(&#39;d-m-Y&#39;, time()) &amp;&amp; $val[&#39;pco&#39;] === &#39;660&#39; ){
        $pdv_message_p .= $val[&#39;link&#39;] . &quot;\n&quot;;
    }
}

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 =&gt; $val ){
  if(date(&#39;d-m-Y&#39;, $val[&#39;confirmed_at&#39;]) === date(&#39;d-m-Y&#39;, time())) { 
    switch ($val[&#39;pco&#39;]) {
      case &#39;620&#39;:
        $pdv_message_b .= $val[&#39;link&#39;] . &quot;\n&quot;;
        break;
      case &#39;650&#39;:     
        $pdv_message_a .= $val[&#39;link&#39;] . &quot;\n&quot;;
        break;
      case &#39;660&#39;:
        $pdv_message_p .= $val[&#39;link&#39;] . &quot;\n&quot;;
        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.)

huangapple
  • 本文由 发表于 2023年2月8日 21:55:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定