0
我有這樣的PHP代碼從數據庫activity
獲取數據:獲取數據,並創建所需的JSON格式
try {
$dateString = '09.03.2014';
$startDate = new DateTime($dateString);
$endDate = clone $startDate;
$startDate = $startDate->format("U") . PHP_EOL;
$period = new DateInterval('P1M');
$endDate->add($period);
$endDate = $endDate->format("U") . PHP_EOL;
$interval = new DateInterval('P1D');
$sql = <<<EOD
SELECT timestamp_day,name FROM activity WHERE timestamp_day BETWEEN $startDate AND $endDate
ORDER BY timestamp_day
EOD;
$data = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$output = ['data' => $data];
$jsonTable = json_encode($output); //$date->format("U") . PHP_EOL;//format your date to timestamp here
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo $jsonTable;
此代碼給我從日期到最新的數據,我得到這個JSON:
data: [{timestamp_day:1394319600, name:Meeting}, {timestamp_day:1394319600, name:Car repair},…]
0: {timestamp_day:1394319600, name:Meeting}
1: {timestamp_day:1394319600, name:Car repair}
2: {timestamp_day:1394406000, name:Travel}
3: {timestamp_day:1394492400, name:Work}
4: {timestamp_day:1394578800, name:Vacation}
現在(從Chrome瀏覽器控制檯複製),你可以看到,我有一個從一些日期兩條記錄(0-1),我希望把同一個對象所以是這樣的:
{timestamp_day:1394319600, name:Meeting, Car repair}
{timestamp_day:1394406000, name:Travel}
{timestamp_day:1394492400, name:Work}
{timestamp_day:1394578800, name:Vacation}
另外我還有一個問題,我現在可以如何使用這個元素becouse我需要在html代碼中:timestamp_day?
而且日期是不要有我的東東擺在JSON所以記錄是這樣的:
{timestamp_day:1394406000, name:'<a>No data</a>'}
一些想法?
請在這裏展示在回答如何使用準備好的語句,還你能告訴我如何添加空行,如果不存在用於數據那天:{timestamp_day:1394406000,名稱:'沒有數據'} – 2014-11-08 17:59:48
我可以在這裏使用間隔,以檢查timestamp_day的日間隔不是每一秒... – 2014-11-08 18:04:19