【问题标题】:php - Putting a for loop inside a variablephp - 在变量中放置一个 for 循环
【发布时间】:2013-02-26 09:45:44
【问题描述】:

我试图在 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 消息一起使用或稍后使用。

【问题讨论】:

    标签: php email variables loops for-loop


    【解决方案1】:

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

    "<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>"
    

    【讨论】:

    • 但我想要 &仅当循环中有更多数据时。还是谢谢。
    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 但问题是他能不能做到。
      • 英语并不是这里所有用户的母语,所以有时你不应该从字面上理解他们的问题,而是想一想“他们可能/实际上想要什么?”
      【解决方案4】:

      试试这个

      $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>";
      

      【讨论】:

      • 工作就像一个魅力。非常感谢。
      【解决方案5】:

      使用这段代码,你错过了打开&lt;?php和关闭?&gt;标签的php

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

      【讨论】:

      • OP 想要追加字符串,而不是在循环中回显某些内容
      • 您不需要&lt;?php?&gt; 部分,因为所有代码都处于“PHP 模式”
      • @Veger 忘记删除了。 :)
      猜你喜欢
      • 1970-01-01
      • 2014-11-24
      • 2010-11-13
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      相关资源
      最近更新 更多