[英]Dynamic PHP Email with All Values from Database Table in HTML using Mysqli
I'm a noob here, but I all I want to do here is to generate a PHP Mail using all the rows and their values from a Database Table using MYSQLi method. 我在这里是菜鸟,但我要做的就是使用MYSQLi方法使用数据库表中的所有行及其值生成PHP邮件。
My Table Format ndr_confirm_id | 我的表格格式ndr_confirm_id | order_id (INT) |
order_id (INT)| tracking_no (BIGINT) |
tracking_no (BIGINT)| status (0,1) |
状态 (0,1)| update_time Estimated Rows in the Table : 50-100
update_time表中的估计行:50-100
I want to generate a table within the PHP Mail which lists values from ALL rows available in the table. 我想在PHP Mail中生成一个表,该表列出了表中所有可用行的值。
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$con=mysqli_connect("localhost","admin","ABC1234!","my_krty");//database connection
$date = date('Y-m-d');
$e_date = date('d-m-Y');
//Email Receiver//
$to = "xxx@example.com";
//From Header//
$header = "From: MyKrty<undelivered@example.com>"."\r\n";
$header .= "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html; charset=ISO-8859-1" . "\r\n";
// Subject //
$subject = "Request";
// Message //
$message = "<html>
<head>
<title></title>
<link href='https://fonts.googleapis.com/css?family=Pacifico|Varela+Round' rel='stylesheet' />
</head>
<body style='font-family: ' varela=''>
<table align='center' border='0' cellpadding='5' style='background-color:#f2f2f2; border-radius:5px; color:#333333; padding:7px; width:85%;'>
<tbody>
<tr>
<td style='text-align: center;'>
<strong>Tracking AWB</strong></td>
<td style='text-align: center;'><strong>
Order ID #</strong></td>
</tr>";
$get_ndr = mysqli_query($con,"SELECT * FROM `oc_ndr_confirm` WHERE `status`='0'");
while ($row = mysqli_fetch_array($get_ndr))
{
$order_id = $row['order_id'];
$tracking_no = $row['tracking_no'];
foreach($row as $value){
$message. = "<tr><td style='text-align:center;'><strong>".$value['tracking_no']."</td><td style='text-align:center;'>".$value['order_id']."</td></tr>";
}}
$message.= " </tbody>
</table>
<p>
</p>
<p>
</p>
<p>
We request you to kindly re-schedule the following above at your earliest.</p>
</body>
</html> \r\n";
//Send Mail//
$mail_send = mail($to,$subject,$message,$header);
I've tried a lot of Threads, but unable to figure out, as I don't understand arrays much. 我已经尝试了很多线程,但是由于我不太了解数组,所以无法弄清楚。 The mail function is working fine, but I'm just receiving one row only in the table, with values as 2 in both columns.
邮件功能运行良好,但是我只在表中收到一行,两列的值为2。
The last thread I referred to was How to Send Email with all Products in the Cart . 我提到的最后一个主题是如何使用购物车中的所有产品发送电子邮件 。
Though, I couldn't make it referring the above mentioned thread as it was too confusing. 虽然,我不能引用上面提到的线程,因为它太混乱了。
Here are 2 small edits to your code. 这是对代码的2个小修改。 First a small error in your html.
首先是您的html中的一个小错误。 Secondly you have a while loop already where you set your db values in your variables.
其次,您已经有一个while循环,您可以在其中设置变量的db值。 No extra foreach loop is needed.
不需要额外的foreach循环。
<body style='font-family: varela'>
while ($row = mysqli_fetch_array($get_ndr)) {
$order_id = $row['order_id'];
$tracking_no = $row['tracking_no'];
$message. = "<tr><td style='text-align:center;'><strong>".$tracking_no."</td><td style='text-align:center;'>".$order_id."</td></tr>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.