...

View Full Version : while-loop in email



kaykay
02-09-2010, 02:21 AM
I'm having some difficulty getting a looped array in a table into the message of a HTML email.
I think I may be approaching it incorrectly. Could someone tell me if this is the correct methodology?



$message="
$inv_fetch = mysql_query("SELECT * FROM table ") or die('Error SQL !'.mysql_error());
<table width='800'>


<tr>
<th colspan='7'><div align='left'>Purchase Order Details: </div></th>
</tr>
<tr>
<td width='61'>Code</td>
<td width='220'>Product</td>
</tr>

while($row3 = mysql_fetch_array($inv_fetch_new))
{
<tr>
<td>$row3["code"]</td>
<td>$row3["prod"]</td>

</tr>
}

</table>
"

Normally this should work, but the fact that it's between two double-quotes makes it not work.

MattF
02-09-2010, 02:29 AM
$inv_fetch = mysql_query("SELECT * FROM table ") or die('Error SQL !'.mysql_error());

$message="<table width='800'>
<tr>
<th colspan='7'><div align='left'>Purchase Order Details:</div></th>
</tr>
<tr>
<td width='61'>Code</td>
<td width='220'>Product</td>
</tr>";

while($row3 = mysql_fetch_array($inv_fetch_new))
{
$message .= "<tr>
<td>$row3["code"]</td>
<td>$row3["prod"]</td>
</tr>";
}
$message .= "</table>";


Should be something like. (I'm crap at code editing in these textarea's).

bdl
02-09-2010, 02:29 AM
<?php
$sql= 'SELECT field1, field2, field3 FROM table';
$result= mysql_query($sql);

$message='<table>';
$message.= '<tr colspan="3"><th>heading</th></tr>'

while($row= mysql_fetch_assoc($result) ) {
$message.= '<tr>';
$message.= "<td>{$row['field1']}</td>";
$message.= "<td>{$row['field2']}</td>";
$message.= "<td>{$row['field3']}</td>";
$message.= '</tr>';
}
$message.= '</table>';
?>


Note I'm using the concatenation operator '.' to add to the table markup as I go. You could certainly use the HEREDOC syntax if you wanted with some portion of the output, but you do have to escape the string at some point to run programatically through the data.

kaykay
02-09-2010, 02:39 AM
you guys rock!

Your code snippets make much more sense! I had a feeling that I was going about it the wrong way

thankyou!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum