I am trying to create a Web Service using PHP. My Data is coming from SQL Server Database. The Query output that I want to use is coming from an inner join. There are multiple rows for same day (Date Column). These are later grouped based on date in the json output.
I have not much exposure to use PHP, and I like to learn any ways that I could create a Nested JSON output as below from the SQL Data.
[
{
"date": "2018-11-09 18:30:00",
"details": [{
"ServerName":"Server1",
"ScheduleStart":"2018-11-09 08:00:00",
"ScheduleEnd": "2018-11-09 11:00:00"
},
{
"ServerName":"Server2",
"ScheduleStart":"2018-11-09 18:00:00",
"ScheduleEnd": "2018-11-09 21:00:00"
},
{
"ServerName":"Server3",
"ScheduleStart":"2018-11-09 21:00:00",
"ScheduleEnd": "2018-11-10 00:00:00"
}
],
"total":3,
"summary":[{
"ServerName": "Server1",
"Status": "Success"
},
{
"ServerName": "Server2",
"Status": "Failed"
},
{
"ServerName": "Server3",
"Status": "Scheduled"
}
]
},
{
"date": "2018-11-10 18:30:00",
"details": [{
"ServerName":"Server3",
"ScheduleStart":"2018-11-09 21:00:00",
"ScheduleEnd": "2018-11-10 00:00:00"
},
{
"ServerName":"Server4",
"ScheduleStart":"2018-11-10 02:00:00",
"ScheduleEnd": "2018-11-10 05:00:00"
}
],
"total":2,
"summary":[{
"ServerName": "Server3",
"Status": "Success"
},
{
"ServerName": "Server4",
"Status": "Scheduled"
}
]
}
]
Please guide me how to start. I have explored a few ways, but nothing has given me enough confidence to create similar output.
The json_encode gives me plain json as below. But, I actually want the nested output as already showed above.
[
{
"date": "2018-05-27 00:00:00.000",
"ServerName": "Server1",
"ScheduleStart": "2018-05-27 03:00:00.000",
"ScheduleEnd": "2018-05-27 06:00:00.000",
"Status": "Scheduled"
},
{
"date": "2018-05-27 00:00:00.000",
"ServerName": "Server2",
"ScheduleStart": "2018-05-27 21:30:00.000",
"ScheduleEnd": "2018-05-28 00:30:00.000",
"Status": "Scheduled"
}
]
I use SQL 2014 which doesn't allow nested JSON output from SQL, which would have been lot easier in this case.
Or, I believe, its possible to do multiple calls to Database to collect and create the array, which doesn't look like a good practice.
