0

我正在尝试在 html 邮件消息中回显一个 for 循环,该循环是

for($i=0; $i<$arrlength; $i++)
{
echo $mailroom[$i] ;
if ($i<($arrlength-1) )
{
echo " &amp; ";
}
}

它完美地打印了结果,但它没有在 html 消息中打印任何结果,html 消息是

$headers = "From: ". "XXXX" . "<" . $frommail . ">\r\n";
$headers .= "Reply-To: " . $frommail . "\r\n";
$headers .= "Return-path: ". $frommail;
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";


$sendmessage = "
<html>
<head>
<title>XXXXXX DETAILS</title>
</head>
<body>
<p>DATA FOR XXXXXXXXXX</p>
<table border=1>
<tr>
<th>Booked on</th>
<th>Name</th> 
<th>Bank</th>
<th>UIN</th>
<th>Phone</th>
<th>From</th>
<th>To</th>
<th>Room No.s</th>
<th>Tariff</th>
<th>Caution Money</th>
<th>Courier</th>
<th>Bank Charges</th>
<th>Total Received</th>
</tr>
<tr>
<td>$mailtoday</td>
<td>$name (ESP)</td>
<td>&nbsp;</td>
<td>$uin</td>
<td>$phone</td>
<td>$mailfrom</td>
<td>$mailto</td>
<td>
**for($i=0; $i<$arrlength; $i++)
{
echo $mailroom[$i] ;
if ($i<($arrlength-1) )
{
echo " &amp; ";
}
}**
    </td>
<td>$room_total</td>
<td>$c_money</td>
<td>$courier</td>
<td>$b_charges</td>
<td>$totalreceived</td>
</tr>
</table>
</body>
</html>
";
`

我可以将 for 循环放在一个变量中,以便我可以将它与 html 消息一起使用,或者稍后再使用它。

4

5 回答 5

2

在尝试使用非字符串值之前,您必须关闭字符串。在这种情况下,我会这样做:

"<td>$name (ESP)</td>
<td>&nbsp;</td>
<td>$uin</td>
<td>$phone</td>
<td>$mailfrom</td>
<td>$mailto</td>
<td>" . implode(' &amp; ', $mailroom) . "</td>
<td>$room_total</td>
<td>$c_money</td>"
于 2013-02-26T09:50:24.553 回答
0

您不能for在字符串中使用 -loop(或任何其他语句)。

相反,您需要在循环内连接您的字符串。例如:

$myString = "test ";
for($i = 0; $i < 3; $i++) {
  $myString = $myString . "$i, ";
}
$myString = $myString . " end!";
echo $myString; // shows "test 1, 2, 3, end!"

(我创建了这个小例子,因为你的代码片段很长,但同样适用)

于 2013-02-26T09:50:50.080 回答
0

使用这段代码,你错过了 php 的开始<?php和结束?>标签

$sendmessage .= ".........<td>";
for($i=0; $i<$arrlength; $i++)
{
    $sendmessage .= $mailroom[$i] ;
    if ($i<($arrlength-1) )
     {
       $sendmessage .= " &amp; ";
   }
}
$sendmessage .= "</td>.........";
于 2013-02-26T09:48:21.833 回答
0

不,你不能这样做。双引号用于变量替换。不适用于代码运行。

于 2013-02-26T09:52:27.077 回答
0

尝试这个

$headers = "From: ". "XXXX" . "<" . $frommail . ">\r\n";
$headers .= "Reply-To: " . $frommail . "\r\n";
$headers .= "Return-path: ". $frommail;
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";


$sendmessage = "
<html>
<head>
<title>XXXXXX DETAILS</title>
</head>
<body>
<p>DATA FOR XXXXXXXXXX</p>
<table border=1>
<tr>
<th>Booked on</th>
<th>Name</th> 
<th>Bank</th>
<th>UIN</th>
<th>Phone</th>
<th>From</th>
<th>To</th>
<th>Room No.s</th>
<th>Tariff</th>
<th>Caution Money</th>
<th>Courier</th>
<th>Bank Charges</th>
<th>Total Received</th>
</tr>
<tr>
<td>$mailtoday</td>
<td>$name (ESP)</td>
<td>&nbsp;</td>
<td>$uin</td>
<td>$phone</td>
<td>$mailfrom</td>
<td>$mailto</td>
<td>";
for($i=0; $i<$arrlength; $i++)
{
$sendmessage.= $mailroom[$i] ;
if ($i<($arrlength-1) )
{
$sendmessage .= "&amp; ";
}
}

$sendmessage.=" </td>
<td>$room_total</td>
<td>$c_money</td>
<td>$courier</td>
<td>$b_charges</td>
<td>$totalreceived</td>
</tr>
</table>
</body>
</html>";
于 2013-02-26T09:59:14.187 回答