我需要使用php邮件功能发送邮件。需要数的早餐,午餐,晚餐,晚餐,午夜 (从今天到接下来的7天),每个字段标记为" 1&#34 ;.
Mysql表和数据
每天(从今天到接下来的7天)需要早餐,午餐,晚餐,晚餐,午夜的计数,其中每个字段都标记为& #34; 1"
要循环的最简单的查询: -
从foodPlan中选择计数(BREAKFAST),其中BREAKFAST = 1且日期=" 2014-09-17&#34 ;;
通过这种方式,从今天开始连续7天 专栏 - 早餐,午餐,晚餐,晚餐,午夜 - 每个应提供标记为" 1"需要计算。
结果需要用这些数据邮寄: -
$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 );
答案 0 :(得分:0)
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
答案 1 :(得分:0)
希望这会对你有所帮助。
// 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>";
这将必须显示您想要的表格。