如何在交叉表中格式化或显示

时间:2015-11-02 01:52:52

标签: php mysql

这是我的查询中的结果,如何在交叉表中显示?

{"data":[{"thedate":"2015-10-09","time_slot":"00:00 - 01:00","thecount":"0"},{"thedate":"2015-10-09"
,"time_slot":"01:00 - 02:00","thecount":"0"},{"thedate":"2015-10-09","time_slot":"02:00 - 03:00","thecount"
:"0"},{"thedate":"2015-10-09","time_slot":"03:00 - 04:00","thecount":"0"},{"thedate":"2015-10-09","time_slot"
:"04:00 - 05:00","thecount":"0"},{"thedate":"2015-10-09","time_slot":"05:00 - 06:00","thecount":"0"}
,{"thedate":"2015-10-09","time_slot":"06:00 - 07:00","thecount":"0"},{"thedate":"2015-10-09","time_slot"
:"07:00 - 08:00","thecount":"0"},{"thedate":"2015-10-09","time_slot":"08:00 - 09:00","thecount":"0"}
,{"thedate":"2015-10-09","time_slot":"09:00 - 10:00","thecount":"0"},{"thedate":"2015-10-09","time_slot"
:"10:00 - 11:00","thecount":"0"},{"thedate":"2015-10-09","time_slot":"11:00 - 12:00","thecount":"0"}
,{"thedate":"2015-10-09","time_slot":"12:00 - 13:00","thecount":"0"},{"thedate":"2015-10-09","time_slot"
:"13:00 - 14:00","thecount":"0"},{"thedate":"2015-10-09","time_slot":"14:00 - 15:00","thecount":"0"}
,{"thedate":"2015-10-09","time_slot":"15:00 - 16:00","thecount":"0"},{"thedate":"2015-10-09","time_slot"
:"16:00 - 17:00","thecount":"0"},{"thedate":"2015-10-09","time_slot":"17:00 - 18:00","thecount":"0"}
,{"thedate":"2015-10-09","time_slot":"18:00 - 19:00","thecount":"0"},{"thedate":"2015-10-09","time_slot"
:"19:00 - 20:00","thecount":"1"},{"thedate":"2015-10-09","time_slot":"20:00 - 21:00","thecount":"0"}
,{"thedate":"2015-10-09","time_slot":"21:00 - 22:00","thecount":"0"},{"thedate":"2015-10-09","time_slot"
:"22:00 - 23:00","thecount":"0"},{"thedate":"2015-10-09","time_slot":"23:00 - 24:00","thecount":"0"}
,{"thedate":"2015-10-25","time_slot":"00:00 - 01:00","thecount":"0"},{"thedate":"2015-10-25","time_slot"
:"01:00 - 02:00","thecount":"0"},{"thedate":"2015-10-25","time_slot":"02:00 - 03:00","thecount":"0"}
,{"thedate":"2015-10-25","time_slot":"03:00 - 04:00","thecount":"0"},{"thedate":"2015-10-25","time_slot"
:"04:00 - 05:00","thecount":"0"},{"thedate":"2015-10-25","time_slot":"05:00 - 06:00","thecount":"0"}
,{"thedate":"2015-10-25","time_slot":"06:00 - 07:00","thecount":"0"},{"thedate":"2015-10-25","time_slot"
:"07:00 - 08:00","thecount":"0"},{"thedate":"2015-10-25","time_slot":"08:00 - 09:00","thecount":"0"}
,{"thedate":"2015-10-25","time_slot":"09:00 - 10:00","thecount":"0"},{"thedate":"2015-10-25","time_slot"
:"10:00 - 11:00","thecount":"0"},{"thedate":"2015-10-25","time_slot":"11:00 - 12:00","thecount":"0"}
,{"thedate":"2015-10-25","time_slot":"12:00 - 13:00","thecount":"0"},{"thedate":"2015-10-25","time_slot"
:"13:00 - 14:00","thecount":"0"},{"thedate":"2015-10-25","time_slot":"14:00 - 15:00","thecount":"0"}
,{"thedate":"2015-10-25","time_slot":"15:00 - 16:00","thecount":"0"},{"thedate":"2015-10-25","time_slot"
:"16:00 - 17:00","thecount":"0"},{"thedate":"2015-10-25","time_slot":"17:00 - 18:00","thecount":"0"}
,{"thedate":"2015-10-25","time_slot":"18:00 - 19:00","thecount":"0"},{"thedate":"2015-10-25","time_slot"
:"19:00 - 20:00","thecount":"3"},{"thedate":"2015-10-25","time_slot":"20:00 - 21:00","thecount":"4"}
,{"thedate":"2015-10-25","time_slot":"21:00 - 22:00","thecount":"4"},{"thedate":"2015-10-25","time_slot"
:"22:00 - 23:00","thecount":"4"},{"thedate":"2015-10-25","time_slot":"23:00 - 24:00","thecount":"4"}
]}

这是我想要的输出。请有人帮助我。

+---------------+------------+------------+
|     TIME      | 2015-10-09 | 2015-10-25 |
+---------------+------------+------------+
| 00:00 - 01:00 |          0 |          0 |
| 01:00 - 02:00 |          0 |          0 |
| 02:00 - 03:00 |          0 |          0 |
| 03:00 - 04:00 |          0 |          0 |
| 04:00 - 05:00 |          0 |          0 |
| 05:00 - 06:00 |          0 |          0 |
| 06:00 - 07:00 |          0 |          0 |
| 07:00 - 08:00 |          0 |          0 |
| 08:00 - 09:00 |          0 |          0 |
| 09:00 - 10:00 |          0 |          0 |
| 10:00 - 11:00 |          0 |          0 |
| 11:00 - 12:00 |          0 |          0 |
| 12:00 - 13:00 |          0 |          0 |
| 13:00 - 14:00 |          0 |          0 |
| 14:00 - 15:00 |          0 |          0 |
| 15:00 - 16:00 |          0 |          0 |
| 16:00 - 17:00 |          0 |          0 |
| 17:00 - 18:00 |          0 |          0 |
| 18:00 - 19:00 |          0 |          0 |
| 19:00 - 20:00 |          1 |          3 |
| 20:00 - 21:00 |          0 |          4 |
| 21:00 - 22:00 |          0 |          4 |
| 22:00 - 23:00 |          0 |          4 |
| 23:00 - 24:00 |          0 |          4 |
+---------------+------------+------------+

提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试这种方法:

$ json =您的数据;

$decoded = json_decode($json);
$arr = array();

/**GROUP YOUR DATA BY TIME AND DATE**/
foreach($decoded->data as $key => $value) {
$arr[$value->time_slot][$value->thedate] = $value->thecount;
}

echo "<pre>";

$table = "<table>";
$ct = 0;
foreach($arr as $time => $dates) {

/**CREATE HEADER DINAMICALLY**/
if($ct == 0) {
 $table .= "<tr><td>TIME</td>";
 foreach(array_keys($dates) as $header) {
     $table .= "<td>".$header."</td>";
  }
 $table .= "</tr>";
 $ct = 1;
}

/**CREATE VALUES DINAMICALLY**/
$table .= "<tr>";
$table .= "<td>" . $time . "</td>";
foreach($dates as $date) {
        $table .= "<td>".$date."</td>";
}
$table .= "</tr>";


}

$table .= "</table>";
如果这是你的目标,那么

$ table就是你的html输出。使用此解决方案,您的JSON数据可能包含您想要的日期数。