我使用下面的代码发送报告,但是当我添加else
语句时,即使if语句为真,也会返回所有语句。
foreach ( $result as $page ) {
$date1 = new DateTime($page->start_date);
$date2 = new DateTime($page->end_date);
if (strtotime($page->start_date) >= strtotime('today') && strtotime($page->start_date) < strtotime('tomorrow')) {
$email_content .= '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>' . $page->post_title . ' </strong></span>';
$email_content .= '<span style=" font-size: 1em; font-family: arial, sans-serif; color:#00b200;"><strong>Order By ' . $date1->format('d-m-y') . ' ' . '</strong></span>';
$email_content .= '<div style=" font-size: 1em; font-family: arial, sans-serif; color:#cc0000;"><strong>For Delivery ' . $date2->format('d-m-y') . '</strong></div><br>' . '<br>';
}else {
$email_content = '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>tester </strong></span>';
}
}
感谢阅读:)
答案 0 :(得分:1)
当你循环时,你的$ email_content变量会在else语句中被覆盖。你附加的if是。但在别的地方覆盖。尝试将代码更新为:
foreach ( $result as $page ) {
$date1 = new DateTime($page->start_date);
$date2 = new DateTime($page->end_date);
if (strtotime($page->start_date) >= strtotime('today') && strtotime($page->start_date) < strtotime('tomorrow')) {
$email_content .= '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>' . $page->post_title . ' </strong></span>';
$email_content .= '<span style=" font-size: 1em; font-family: arial, sans-serif; color:#00b200;"><strong>Order By ' . $date1->format('d-m-y') . ' ' . '</strong></span>';
$email_content .= '<div style=" font-size: 1em; font-family: arial, sans-serif; color:#cc0000;"><strong>For Delivery ' . $date2->format('d-m-y') . '</strong></div><br>' . '<br>';
}else {
$email_content .= '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>tester </strong></span>';
}
}