Using while loop to fetch details in Swift Mailer

心已入冬 提交于 2020-01-06 07:07:01

问题


In my code, I want to include while loop to fetch info from DB & send it in a table form to user. I tried to search numerous articles but no solutions worked for me actually. Here's my code:

$message->setBody('
<html>
<body>
<table style="margin-top:10px; width: 680px; border:0px;">
<tr>
    <th width="80%">Product Details</th>
    <th width="20%">Amount</th>
</tr>'); /* This is Line 43 */
while ($row = mysql_fetch_array($results2)){
$message->setBody .= ('<tr>
    <th width="80%">'.$row["product_name"].'&nbsp-&nbsp'.
                      $row["quantity"].'&nbsp'.$row["type"].'</th>
    <th width="20%">&#8377;&nbsp;'.$row["subtotal"].'</th>
</tr>');
}
$message->setBody .= ('</table>
</body>
</html>',
'text/html');

Error that is coming with this is:

Parse error: syntax error, unexpected ';' in /home/public_html/example.com/
test.php on line 43

I know I must be missing something basic but not able to find out. Any help will be appreciated.

EDIT

Results are coming fine from while loop (tested outside email), so that can not be the issue.

ERROR IN LAST PART

$message->setBody .= ("</table>
</body>
</html>",
'text/html');

Error is " 'Unexpected ',' in file at line no. 62 ".


回答1:


Firstly, you have some syntax errors in your code. One place you are calling $message->setBody as function and one place you use it as object property. Secondly, if have below working version for you. Last, in future - read more carefully you code and try to understand what are you doing during developing. Your code has parts that doesn't make any sense.

<?php 

$html = "
    <html>
        <body>
            <table style='margin-top:10px; width: 680px; border:0px;'>
                <thead>
                    <tr>
                        <th width='80%'>Product Details</th>
                        <th width='20%'>Amount</th>
                    </tr>
                </thead>
                <tbody>";
while ($row = mysql_fetch_array($results2)) {
    $html .= "
                    <tr>
                        <td width='80%'>{$row["product_name"]}&nbsp-&nbsp{$row["quantity"]}&nbsp{$row["type"]}</td>
                        <td width='20%'>&#8377;&nbsp;{$row["subtotal"]}</td>
                    </tr>";
}
$html .= "
                </tbody>
            </table>
        </body>
    </html>";
$message->setBody($html, "text/html");

?>


来源:https://stackoverflow.com/questions/22652703/using-while-loop-to-fetch-details-in-swift-mailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!