简体   繁体   English

PHP MySQL获取日期并将每个月的数据拆分为JSON

[英]PHP MySQL get dates and split data into JSON for each month

I have an MySQL -database with a table which contains a lot of events, eg concerts. 我有一个MySQL数据库,该数据库的表包含很多事件,例如音乐会。 The structure of the table is pretty simple: 该表的结构非常简单:

id (int, 11, pk , ai)
event_name (varchar, 255)
event_location (varchar, 255)
event_date (date)

Now, for display purpose on the frontend, I would like to show the events in some kind of "month overview", like: 现在,出于前端显示的目的,我想以某种“月概述”来显示事件,例如:

January
- event blabla  10/01/17
- event thisandthat  17/01/17

February
- event something 05/02/17
- event another something 13/02/17

...etc etc

So I was thinking about how to achieve this. 所以我在考虑如何实现这一目标。 Should I create another table and give any month an ID, and also add an "month_id" field in the events-table? 我应该创建另一个表并给任何月份指定一个ID,并在事件表中添加一个“ month_id”字段吗?

I would like to do some json_encode so that in the end I will have an JSON -array with objects which has all the data. 我想做一些json_encode这样最后我将得到一个JSON -array,其中包含所有数据的对象。

Any suggestions? 有什么建议么? Or even better: any examples? 甚至更好:有什么例子吗?

** UPDATE ** **更新**

My desired result should look something like this: 我期望的结果应如下所示:

[{
    "January": [{
            "event_name": "blabla",
            "event_location" : "somewhere",
            "event_date": "2017-01-10" 
        },
        {
            "event_name": "blabla",
            "event_location" : "somewhere",
            "event_date": "2017-01-17"
        }
    ]
},
{
    "February": [{
            "event_name": "blabla",
            "event_location" : "somewhere",
            "event_date": "2017-02-10"
        },
        {
            "event_name": "blabla",
            "event_location" : "somewhere",
            "event_date": "2017-02-17"
        }
    ]
},
{
    "March": [{
            "event_name": "blabla",
            "event_location" : "somewhere",
            "event_date": "2017-03-10"
        },
        {
            "event_name": "blabla",
            "event_location" : "somewhere",
            "event_date": "2017-03-17"
        }
    ]
  }
]
$sql="SELECT MONTH(event_date) event_month, * FROM events ORDER BY event_date";

Then enter in a foreach loop with results comparing if event_month is null (first loop) or the same of the previous loop. 然后进入一个foreach循环,其结果将比较event_month是否为null(第一个循环)或与前一个循环相同。 If it is the same your into the month events subset otherwise you're starting a new month. 如果相同,则进入月份事件子集,否则您将开始新的月份。

$currentmonth=null;
for($row in $rows){
    if($row["event_month"]!=$currentmonth){
        echo "Month ".$row["event_month"];
        $row["event_month"]=$currentmonth;      
    }
    echo "\tevent ".$row["event_name"];
}

You can go ahead and extract the month in your query, something like this 您可以继续提取查询中的月份,例如:

SELECT event_name, event_location, event_date,
       DATE_FORMAT(event_date, '%Y - %M') AS event_month
WHERE ...
ORDER BY event_date

Whatever format works for you. 无论哪种格式都可以为您服务。 Including the year may or may not be useful, depending on what you're doing with it. 包括年份可能有用也可能没有用,这取决于您使用的年份。 If you don't need the year included, you can just use MONTH or MONTHNAME instead. 如果您不需要包含年份,则可以改用MONTH或MONTHNAME。

Then group by that as you fetch the rows from your query result. 然后根据从查询结果中获取的行进行分组。

<?php

while ($row = $stmt->yourFavoriteFetchMethod()) {
    $result[$row->event_month][] = $row;
}

echo json_encode($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM