生成电子邮件时,我很难让foreach循环正常运行。 我已经搜索了答案,并且我找到了将foreach循环插入电子邮件的方向,但没有关于循环无效的说明。
我正在尝试在下订单后将订单收据通过电子邮件发送给客户,除了项目列表外,电子邮件中的所有内容都会发送。
我将尝试尽可能多地包含我的逻辑:下订单时,购物车项目数组将被序列化,然后存储在数据库中。 在支付处理器返回支付成功后,我在网站上显示用户可以打印的收据页面。 订单项从数据库中检索,如下所示:
$stmt = $dbh->prepare("SELECT * FROM *table* WHERE orderID = :q0") $stmt->bindValue(":q0",$orderNum) $stmt->execute(); foreach($stmt as $row) { $items = unserialize($row['order_contents']); }此时,如果我回显$ items,我会看到订单中的一系列项目。 到现在为止还挺好。
现在,因为我正在使用PDO,查询中的foreach循环不是我想出来的方法。 相反,我已经初始化了一个变量来保存foreach循环中的项目:
foreach($items as $prod_id => $value) { $q .= $prod_id . ','; } $q = substr($q, 0, -1);这将数组存储在一个变量中,我现在可以在PDO语句中用于下一个查询:
$stmt2 = $dbh->prepare("SELECT * FROM *productTable* WHERE prod_ID IN ($q)"); $stmt2->execute(); foreach($stmt2 as $row) { $itemName = htmlentities(strip_tags($row['prod_name'])); $id = htmlentities(strip_tags($row['prod_ID'])); $qty = htmlentities(strip_tags($items[$prod_id]['quantity'])); $price = $items[$prod_id]['price']; echo " <tr> <td>".$qty."</td> <td>".$itemName."</a></td> <td>".$price."</td> </tr>"; } // THIS CODE WORKS. I can see each item from the order as a new row in the table再一次,到目前为止一切顺利。 我可以运行一个foreach循环来将项目回显到页面,它们都显示正确的项目名称以及我从该示例中遗漏的表格中的其他信息。
跳到文件的末尾,我有以下脚本不起作用,我无法弄清楚原因。 电子邮件发送成功,但foreach循环中的所有产品均未显示在电子邮件中。
$to = 'user@email.com' $subject = 'Your order receipt for order '.$orderNum $headers = 'From: orders@website.com'."\r\n". 'Reply-To: orders@website.com'."\r\n". 'Content-Type: text/html; charset=UTF-8'."\r\n". 'X-Mailer: PHP/'.phpversion(); $message = '<html><body>'; $message .= '<h2>Thanks for your purchase!</h2><br>'; $message .= '<p>Here are the details of your order number '.$orderRef.':<br><table>'; $message .= ' <tr style="width: 100%;"> <th scope="col" style="width: 20%;">Quantity:</th> <th scope="col" style="width: 60%;">Name:</th> <th scope="col" style="width: 20%;">Price:</th> </tr> '; foreach ($stmt2 as $row) { $message .= " <tr> <td>".$qty."</td> <td>".$itemName."</td> <td>".$price."</td> </tr>"; //THIS CODE DOESN'T WORK, Even though it's using the same variables as the loop from above. } $message .= '</table><br>'; $message .= 'If you have any questions about your order, please call us at <b>XXX-XXX-XXXX</b>'; $message .= '</body></html>'; mail($to, $subject, $message, $headers);电子邮件发送,我看到foreach循环之前和之后的所有内容,但我没有看到生成的实际项目。 在电子邮件代码之后,我可以将变量回显到页面并且它们都正确显示,因此仍然声明变量。
可能相关或不相关的其他信息:我正在尝试使用Ubuntu 14.04在新映像的VPS上执行此代码。 我刚刚在星期一安装了SSL证书,我最近在我的VPS上安装了UFW。 SMTP,IMAP和UTP端口未被阻止。 PHP版本5.5.9-1ubuntu4.21
这是电子邮件输出的屏幕截图:
编辑:变量已在第一个foreach($ stmt2 as $ row)语句之后定义。 我添加了额外的代码来反映这一点。
I'm having difficulty getting a foreach loop to function correctly when generating an email. I've searched for answers and I've found directions to insert a foreach loop into an email, but nothing about the loop not working.
I'm trying to email order receipts to customers after they place an order, and everything in the email gets delivered except for the list of items.
I'll try to include as much of my logic as possible: When the order is placed, the array of shopping cart items is serialized and then stored in the database. After the payment processor returns that the payment was successful, I display a receipt page on the website which the user can print. The order items are retrieved from the database as follows:
$stmt = $dbh->prepare("SELECT * FROM *table* WHERE orderID = :q0") $stmt->bindValue(":q0",$orderNum) $stmt->execute(); foreach($stmt as $row) { $items = unserialize($row['order_contents']); }at this point, if I echo $items, I see an array of the items from the order. So far, so good.
Now because I'm using PDO, a foreach loop within the query isn't something I've figured out how to do. Instead, I've initialized a variable to hold the items from the foreach loop:
foreach($items as $prod_id => $value) { $q .= $prod_id . ','; } $q = substr($q, 0, -1);This stores the array in a variable which I can now use in the PDO statement for the next query:
$stmt2 = $dbh->prepare("SELECT * FROM *productTable* WHERE prod_ID IN ($q)"); $stmt2->execute(); foreach($stmt2 as $row) { $itemName = htmlentities(strip_tags($row['prod_name'])); $id = htmlentities(strip_tags($row['prod_ID'])); $qty = htmlentities(strip_tags($items[$prod_id]['quantity'])); $price = $items[$prod_id]['price']; echo " <tr> <td>".$qty."</td> <td>".$itemName."</a></td> <td>".$price."</td> </tr>"; } // THIS CODE WORKS. I can see each item from the order as a new row in the tableOnce again, so far so good. I can run a foreach loop to echo the items to the page, and they all display with the proper item name as well as other information from the table which I left out from this example.
Skip ahead to the end of the file, and I've got the following script which does not work and I can't figure out why. The email sends successfully, but none of the products from the foreach loop show up in the email.
$to = 'user@email.com' $subject = 'Your order receipt for order '.$orderNum $headers = 'From: orders@website.com'."\r\n". 'Reply-To: orders@website.com'."\r\n". 'Content-Type: text/html; charset=UTF-8'."\r\n". 'X-Mailer: PHP/'.phpversion(); $message = '<html><body>'; $message .= '<h2>Thanks for your purchase!</h2><br>'; $message .= '<p>Here are the details of your order number '.$orderRef.':<br><table>'; $message .= ' <tr style="width: 100%;"> <th scope="col" style="width: 20%;">Quantity:</th> <th scope="col" style="width: 60%;">Name:</th> <th scope="col" style="width: 20%;">Price:</th> </tr> '; foreach ($stmt2 as $row) { $message .= " <tr> <td>".$qty."</td> <td>".$itemName."</td> <td>".$price."</td> </tr>"; //THIS CODE DOESN'T WORK, Even though it's using the same variables as the loop from above. } $message .= '</table><br>'; $message .= 'If you have any questions about your order, please call us at <b>XXX-XXX-XXXX</b>'; $message .= '</body></html>'; mail($to, $subject, $message, $headers);The email sends, and I see everything before and after the foreach loop, but I don't see the actual items being generated. After the email code, I can echo the variables to the page and they all display properly, so the variables are still declared.
Additional information which may or may not be relevant: I'm trying to execute this code on a freshly imaged VPS with Ubuntu 14.04. I just installed an SSL certificate on Monday I installed UFW on my VPS recently. SMTP, IMAP, and UTP ports are not blocked. PHP version 5.5.9-1ubuntu4.21
Here's a screenshot of the email output:
Edit: The variables were already defined after the first foreach($stmt2 as $row) statement. I've added the additional code to reflect this.
最满意答案
$stmt2不是数组,它是一个生成器。 除此之外,这允许仅根据需要将行加载到内存中,而不是需要立即将整个内容读入内存。
问题是,你无法foreach两次。 您需要将其转换为实际数组。 我没有测试,但你可以使用$itemdata = iterator_to_array($stmt2)来做到这一点 - 如果你的第一个foreach里面只有$itemdata[] = $row会让你稍后引用这个$itemdata数组。
$stmt2 isn't an array, it's a generator. Among other things, this allows to only load rows into memory as needed, rather than needing to read the whole thing into memory at once.
The catch is, you can't foreach it twice. You will need to convert it to an actual array. I haven't tested but you may be able to do this with $itemdata = iterator_to_array($stmt2) - failing that just $itemdata[] = $row inside your first foreach will allow you to refer to this $itemdata array later.
更多推荐
发布评论