php - 在变量中放入一个for循环

时间:2013-02-26 09:45:45

标签: php email variables loops for-loop

我正在尝试在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消息或以后使用它。

5 个答案:

答案 0 :(得分: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>"

答案 1 :(得分:0)

使用此代码,您错过了php <?php并关闭?>代码

$sendmessage .= ".........<td>";
for($i=0; $i<$arrlength; $i++)
{
    $sendmessage .= $mailroom[$i] ;
    if ($i<($arrlength-1) )
     {
       $sendmessage .= " &amp; ";
   }
}
$sendmessage .= "</td>.........";

答案 2 :(得分:0)

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

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

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

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

答案 3 :(得分:0)

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

答案 4 :(得分: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>";