Php mysql二维数组查询结果以邮件形式发送
我需要发送一封邮件使用php邮件功能。需要计算从今天到未来7天每天的早餐、午餐、晚上、晚餐和午夜,其中每个字段标记为1 Mysql表和数据 需要计算从今天到未来7天每天的早餐、午餐、晚上、晚餐和午夜,其中每个字段标记为1 要循环的最简单查询:- 从foodPlan中选择CountBreaken,其中早餐=1,日期=2014-09-17 这样,从今天到连续7天按行排列 列-早餐、午餐、晚上、晚餐、午夜-每个列应提供标记为1的计数,不需要计数 结果需要连同以下数据一起邮寄:-Php mysql二维数组查询结果以邮件形式发送,php,mysql,email,multidimensional-array,foreach,Php,Mysql,Email,Multidimensional Array,Foreach,我需要发送一封邮件使用php邮件功能。需要计算从今天到未来7天每天的早餐、午餐、晚上、晚餐和午夜,其中每个字段标记为1 Mysql表和数据 需要计算从今天到未来7天每天的早餐、午餐、晚上、晚餐和午夜,其中每个字段标记为1 要循环的最简单查询:- 从foodPlan中选择CountBreaken,其中早餐=1,日期=2014-09-17 这样,从今天到连续7天按行排列 列-早餐、午餐、晚上、晚餐、午夜-每个列应提供标记为1的计数,不需要计数 结果需要连同以下数据一起邮寄:- $li
$link =mysql_connect("172.17.xx.xxx", "username", "password")
or die(mysql_error());
//To select the database
mysql_select_db("dbname") or die(mysql_error());
//To get the current date
$date=date("Y-m-d");
$dates = array();
$dates[0] = $today;
for ($i=1; $i<7; $i++) {
$dates[$i] = date('Y-m-d', strtotime("+$i day"));
}
.....
.....
# This will set the Subject for the Mail
$subject = "FOODIE plan";
$recipient = 'xx@yyyy.com';
# The following details will be updated in the Body part of the mail
$message = "Foodie";
....
....
mail ( $recipient, $subject, $message, $header );
邮件中的数据应如下所示:-
SUM与GROUP BY相结合,提供了SQL中所需的函数
因此,您应该能够使用以下内容:
SELECT
date,
SUM(BREAKFAST) as breakfast_total,
SUM(LUNCH) as lunch_total
FROM foodPlan
WHERE date >= $firstday AND date <= $lastday
GROUP BY date
希望这对你有帮助
// Table columns are putted in the array.
$columnNames = array('BREAKFAST','LUNCH','EVENING','DINNER','MIDNIGHT');
$today = date("Y-m-d");
$dates = array();
$dates[0] = $today;
for ($i=1; $i<7; $i++) {
$dates[$i] = date('Y-m-d', strtotime("+$i day"));
}
$result = array();
foreach ($columnNames as $column) {
foreach ($dates as $date) {
$query = "SELECT count(".$column.") as cnt FROM foodplan WHERE " .$column. "= 1 AND date = '".$date."'";
$res = mysql_query($query);
$row = mysql_num_rows($res);
if ($row > 0) {
$record = mysql_fetch_assoc($record);
$result[$date][$column] = $record['cnt'];
} else {
// if we don't found any record in the case.
$result[$date][$column] = 0;
}
}
}
// creating table to send in email body
$emailMessage = "<table>
<tr>
<th>Date</th>";
foreach ($columnNames as $cKey => $cValue) {
$emailMessage .= "<th>".$cValue."</th>";
}
$emailMessage .= "</tr>";
foreach ($result as $rDate => $rDataValue) {
$emailMessage .= "<tr><td>".$rDate."</td>";
foreach ($columnNames as $col) {
$emailMessage .= "<td>".$rDataValue[$col]."</td>";
}
$emailMessage .= "</tr>";
}
$emailMessage .= "</table>";
这将必须显示您想要的表。请告诉我们您已经做了什么?请查看编辑@Alankar您可以显示您正在获取的数组吗?需要以报表格式打印mysql查询的结果。这看起来像是糟糕的设计