Sudip Dutta
Sudip Dutta

Reputation: 1

php - Putting a for loop inside a variable

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 " &amp; ";
}
}

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>&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>
";
`

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

Answers (5)

Yogesh Suthar
Yogesh Suthar

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 .= " &amp; ";
   }
}
$sendmessage .= "</td>.........";

Upvotes: 0

Nirmal Ram
Nirmal Ram

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

Upvotes: 0

Magic
Magic

Reputation: 1196

No,You can't do this. The double quotation marks is for variable replacement. Not for code running.

Upvotes: 0

Veger
Veger

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

silkfire
silkfire

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

Upvotes: 2

Related Questions