我在 MySQL 中有一个表,其中有一个startdate
和enddate
。
1 行的示例是(忽略整个命名):
eg
startdate -> 20121224
endate -> 20121226
title -> name
etc
我当前的脚本效果很好,并为每一行添加到 JSON,但是.....我想....为每个 mysql 行获取 startdate 和 enddate,对于包含在 startdate 和 enddate 中的每个日期添加到 JSON编码。
因此,而不是上面的 1 行返回示例:
[{
"user": "378",
"date": "UNIX_TIMESTAMP(startdate)",
"title": "name",
"description": "6 Days",
"url": "UNIX_TIMESTAMP(enddate)",
"bmanager": "manager name",
"academic_year": "20120801",
"division": "division name",
"manager_id": "3"
}]
它将为每个日期的一行返回 3 个 JSON:
[{
"user": "378",
"date": "UNIX_TIMESTAMP(1st date)",
"title": "name",
"description": "6 Days",
"url": "UNIX_TIMESTAMP(1st date)",
"bmanager": "manager name",
"academic_year": "20120801",
"division": "division name",
"manager_id": "3"
}, {
"user": "378",
"date": "UNIX_TIMESTAMP(2nd date)",
"title": "name",
"description": "6 Days",
"url": "UNIX_TIMESTAMP(2nd date)",
"bmanager": "manager name",
"academic_year": "20120801",
"division": "division name",
"manager_id": "3"
}, {
"user": "378",
"date": "UNIX_TIMESTAMP(3rd date)",
"title": "name",
"description": "6 Days",
"url": "UNIX_TIMESTAMP(3rd date)",
"bmanager": "manager name",
"academic_year": "20120801",
"division": "division name",
"manager_id": "3"
}, ]
JSON
<?php
header('Content-type: text/json');
$lmanager = $_GET["lmanager"];
$sqldata = mysql_query('
SELECT
requests.user,
UNIX_TIMESTAMP(requests.start_date) AS date,
requests.employee AS title,
requests.days AS description,
UNIX_TIMESTAMP(requests.end_date) AS url,
business.line_manager AS bmanager,
requests.academic_year,
business.academic_year,
business.division,
line_managers.userid AS manager_id
FROM requests
INNER JOIN holiday_entitlement_business_manual AS business ON requests.user=business.userid AND requests.academic_year=business.academic_year
INNER JOIN line_managers ON business.line_manager=line_managers.name
WHERE requests.approved = 1 AND line_managers.userid = '.$lmanager.'
');
$posts = array();
while($row = mysql_fetch_assoc($sqldata))
{
$row['date'] = $row['date'].'000';
$row['url'] = $row['url'].'000';
$row['description'] = $row['description'].' Days';
$posts[] = $row;
}
mysql_free_result($sqldata);
die(json_encode($posts));
?>