Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

5 Answers

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>"
share|improve this answer
1  
+1 for actually using the brain to make the code simplier – Kyborek Feb 26 at 11:23
but I want the &amp; only if there is any more data in the loop. Thanks anyway. – Sudip Dutta Feb 26 at 18:04

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)

share|improve this answer

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

share|improve this answer
Well... there are several answers showing that you can do this... – Veger Feb 26 at 9:54
But the question is whether he can do this. – Magic Feb 27 at 14:48
English is not the native language of all users here, so sometimes you should not take their question literally and think a bit about 'what do they probably/actually want?' – Veger Feb 27 at 18:44
Yes,Thanks for reminding me. – Magic Mar 1 at 1:36

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>";
share|improve this answer
Worked like a charm. Thanks a ton. – Sudip Dutta Feb 26 at 18:00
If that worked then kindly accept my answer. – Nirmal Ram Feb 27 at 0:46

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>.........";
share|improve this answer
3  
this will not work inside a string – redreggae Feb 26 at 9:52
The OP wants to append the string, not echo some stuff in the loop – Veger Feb 26 at 9:53
@redreggae see the edited code. – Yogesh Suthar Feb 26 at 10:18
1  
@Veger see the edited code. – Yogesh Suthar Feb 26 at 10:19
1  
You do not need the <?php and ?> parts, as all the code is in 'PHP mode' – Veger Feb 26 at 10:21
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.