我正在使用PHP创建一个电子邮件,但我需要在其中添加一个PHP循环和条件语句。这是我的代码:
$message = '
<table width="600" border="0" cellspacing="0" cellpadding="5">
<tr>
<td height="50"><strong>Shipping address</strong>
<p>'.$showDelivery['first_name']." ".$showDelivery['surname'].'</p>
<p>'.$showDelivery['address1'].'</p>
<p>'.$showDelivery['address2'].'</p>
<p>'.$showDelivery['address3'].'</p>
<p>'.$showDelivery['town'].'</p>
<p>'.$showDelivery['county'].'</p>
<p>'.$showDelivery['postcode'].'</p>
<p> </p>
<p><strong>Shipping Date</strong> '.$monthName = date("F", mktime(0, 0, 0, $show['shipping_month'], 10)) . " ".$show['shipping_day']." ".$show['shipping_time'].'</p>
</td>
<td width="150" height="50">'.if($show['number_of_people'] == 0) { $show['number_of_people']; }."</td>
</tr>
</table>';
我在上一个表数据中添加了上面的条件,但它没有运行。
有人可以帮忙吗?
非常感谢!
答案 0 :(得分:2)
您已将if
放入字符串中,这是不对的。
试试这种方式......
$message = '
<table width="600" border="0" cellspacing="0" cellpadding="5">
<tr>
<td height="50"><strong>Shipping address</strong>
<p>'.$showDelivery['first_name']." ".$showDelivery['surname'].'</p>
<p>'.$showDelivery['address1'].'</p>
<p>'.$showDelivery['address2'].'</p>
<p>'.$showDelivery['address3'].'</p>
<p>'.$showDelivery['town'].'</p>
<p>'.$showDelivery['county'].'</p>
<p>'.$showDelivery['postcode'].'</p>
<p> </p>
<p><strong>Shipping Date</strong> '.$monthName = date("F", mktime(0, 0, 0, $show['shipping_month'], 10)) . " ".$show['shipping_day']." ".$show['shipping_time'].'</p>
</td>
<td width="150" height="50">';
if($show['number_of_people'] == 0) {
$message .= $show['number_of_people'];
}
$message .=.'</td>
</tr>
</table>';
答案 1 :(得分:0)
您无法使用字符串连接if()
。
如果您在提交$message
之前,请将变量添加到$message
。
例如:
$number = ($show['number_of_people'] == 0) ? $show['number_of_people'] : '';
$message = '.......<td>'.$number.'</td>......';
希望有所帮助:)
答案 2 :(得分:0)
假设你要做的是回显变量$ show,那么你需要将echo函数放入你的代码中。
if($show['number_of_people'] == 0) { $show['number_of_people']; }
应该是
if($show['number_of_people'] == 0) { echo $show['number_of_people']; }
答案 3 :(得分:0)
你可以在开始之前将它声明为变量。
$showPeople = '';
if($show['number_of_people'] === 0){
$showPeople = $show['number_of_people'];
}
$message = '
<table width="600" border="0" cellspacing="0" cellpadding="5">
<tr>
<td height="50"><strong>Shipping address</strong>
<p>'.$showDelivery['first_name']." ".$showDelivery['surname'].'</p>
<p>'.$showDelivery['address1'].'</p>
<p>'.$showDelivery['address2'].'</p>
<p>'.$showDelivery['address3'].'</p>
<p>'.$showDelivery['town'].'</p>
<p>'.$showDelivery['county'].'</p>
<p>'.$showDelivery['postcode'].'</p>
<p> </p>
<p><strong>Shipping Date</strong> '.$monthName = date("F", mktime(0, 0, 0,
$show['shipping_month'], 10)) . " ".$show['shipping_day']."
".$show['shipping_time'].'</p>
</td>
<td width="150" height="50">'. $showPeople ."</td>
</tr>
</table>';