Reputation: 1
I am trying to echo a for loop inside a html mail message, the loop is
for($i=0; $i<$arrlength; $i++)
{
echo $mailroom[$i] ;
if ($i<($arrlength-1) )
{
echo " & ";
}
}
It is printing the results perfectly, but it not printing any result at the html message, the html message is
$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> </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 " & ";
}
}**
</td>
<td>$room_total</td>
<td>$c_money</td>
<td>$courier</td>
<td>$b_charges</td>
<td>$totalreceived</td>
</tr>
</table>
</body>
</html>
";
`
Can I put the for loop inside a variable so that I can use it withing the html message or otherwise later.
Upvotes: 0
Views: 1147
Reputation: 30488
use this code, you missed php opening <?php
and closing ?>
tags
$sendmessage .= ".........<td>";
for($i=0; $i<$arrlength; $i++)
{
$sendmessage .= $mailroom[$i] ;
if ($i<($arrlength-1) )
{
$sendmessage .= " & ";
}
}
$sendmessage .= "</td>.........";
Upvotes: 0
Reputation: 1732
Try this
$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> </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 .= "& ";
}
}
$sendmessage.=" </td>
<td>$room_total</td>
<td>$c_money</td>
<td>$courier</td>
<td>$b_charges</td>
<td>$totalreceived</td>
</tr>
</table>
</body>
</html>";
Upvotes: 0
Reputation: 1196
No,You can't do this. The double quotation marks is for variable replacement. Not for code running.
Upvotes: 0
Reputation: 37915
You cannot use a for
-loop (or any other statement for that matter) in a string.
Instead you need to concatenate your string inside the loop. For example:
$myString = "test ";
for($i = 0; $i < 3; $i++) {
$myString = $myString . "$i, ";
}
$myString = $myString . " end!";
echo $myString; // shows "test 1, 2, 3, end!"
(I created this small example, as you code snippet is quite long, but the same applies)
Upvotes: 0
Reputation: 26033
You have to close off your string before attempting to use a non-string value. In this case I'd do like this:
"<td>$name (ESP)</td>
<td> </td>
<td>$uin</td>
<td>$phone</td>
<td>$mailfrom</td>
<td>$mailto</td>
<td>" . implode(' & ', $mailroom) . "</td>
<td>$room_total</td>
<td>$c_money</td>"
Upvotes: 2