2015-11-02 38 views
1

這是我的查詢結果,我如何在交叉表中顯示這個結果?如何格式化或在交叉表中顯示

{"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 | 
+---------------+------------+------------+ 

在此先感謝您。

+2

你有什麼試過?看起來你剛剛採取了數據轉儲,並希望我們爲你弄明白。 – Teepeemm

+0

@Teepeemm,我不知道如何獲得像我上面發佈的輸出 – jemz

+0

它看起來像你的查詢返回json。這是文檔說你應該期待什麼?看看如何在php中解析json。 – Teepeemm

回答

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數據可能會隨您需要的日期數量一起提供。

+0

我試過你的解決方案,但是在顯示數據時出現問題,第一列日期沒有計數。 – jemz

+0

謝謝你的作品。 – jemz