【问题标题】:mysql two dimensional array query result send as a mailmysql二维数组查询结果作为邮件发送
【发布时间】:2015-05-14 12:03:44
【问题描述】:

我需要使用 php 邮件功能发送邮件。需要count早餐、午餐、晚上、晚餐、午夜每天(从今天到接下来的 7 天),其中每个字段都标记为“1”。

Mysql 表和数据

需要count每天(从今天到接下来的 7 天)的 BREAKFAST、LUNCH、EVENING、DINNER、MIDNIGHT(从今天到接下来的 7 天),其中每个字段都标记为“ 1"。

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

从 foodPlan 中选择 count(BREAKFAST),其中 BREAKFAST=1 和 date="2014-09-17";

这样,从今天到连续 7 天逐行 列 - BREAKFAST、LUNCH、EVENING、DINNER、MIDNIGHT - 每个列都应提供标记为“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 );

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

【问题讨论】:

  • 请告诉我们你做了什么?
  • 请查看编辑@Alankar
  • 你能显示你得到的数组吗?
  • 需要将mysql查询的结果打印成报告格式。
  • 这看起来很糟糕的设计

标签: php mysql email multidimensional-array foreach


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    希望这会对你有所帮助。

    // 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>";
    

    这必须显示你想要的表格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      相关资源
      最近更新 更多