mysql二维数组查询结果作为邮件发送

时间:2015-03-12 11:00:50

标签: php mysql email multidimensional-array foreach

我需要使用php邮件功能发送邮件。需要的早餐,午餐,晚餐,晚餐,午夜 (从今天到接下来的7天),每个字段标记为" 1&#34 ;.

Mysql表和数据

enter image description here

每天(从今天到接下来的7天)需要早餐,午餐,晚餐,晚餐,午夜计数,其中每个字段都标记为& #34; 1"

要循环的最简单的查询: -

从foodPlan中选择计数(BREAKFAST),其中BREAKFAST = 1且日期=" 2014-09-17&#34 ;;

enter image description here

通过这种方式,从今天开始连续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 );

数据在邮件中应如下所示: -

enter image description here

2 个答案:

答案 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>";

这将必须显示您想要的表格。