Reputation: 3
I'm very much a beginner so please bear with me. I am trying to display a JSON response in a table. I have retrieved the JSON data ok but the structure is a bit complex for me to traverse to get the data I want to display.
It is a school timetable so the format I am going for is below:
Session | Monday | Tuesday | Wednesday
-----------------------------------------------------------
Home Room | 2HRS - JS1.6 | 2HRS - JS1.6 | ....
08:30 - 08:45 | Homeroom | Homeroom | ....
| Ms Jones | Ms Jones | ....
| | |
Period 1 | 2ENS - JS1.6 | 2ENS - JS1.6 | ....
08:45 - 09:35 | English | English | ....
| Ms Jones | Ms Jones | ....
First thing I am trying to do is loop through the rooms and display them in a table.
This is the beginning of my JSON response:
{
"data": [
{
"description": "Home Room",
"from_time": "08:30",
"to_time": "08:45",
"timetable": {
"1": {
"DayNumber": "1",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
"2": {
"DayNumber": "2",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
"3": {
"DayNumber": "3",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
"4": {
"DayNumber": "4",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
"5": {
"DayNumber": "5",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
}
}
},
{
"description": "Period 1",
"from_time": "08:45",
"to_time": "09:35",
"timetable": {
"1": {
"DayNumber": "1",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:45",
"EndTime": "09:35"
},
"2": {
"DayNumber": "2",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:45",
"EndTime": "09:35"
},
"3": {
"DayNumber": "3",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:45",
"EndTime": "09:35"
},
"4": {
"DayNumber": "4",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:45",
"EndTime": "09:35"
},
"5": {
"DayNumber": "5",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:45",
"EndTime": "09:35"
}
}
}
]}
Here is some of my code.
$response = json_decode($get_data, true);
$rooms = $response['data']['0']['timetable'];
?>
<?php foreach ($rooms['1'] as $v) { ?>
<tr>
<td></td>
<td class="first"><?php echo $v;} ?></td>
</tr>
This loops through the "1" array and displays the following data on each row. I just can't seem to navigate far enough down the structure to access to room.
1
2HRS
Homeroom
JS.1.6
Yasemin Sali
Ms
Sali
08:30
08:45
I may be totally off track here so any guidance would be greatly appreciated.
***************** EDIT **********************************
Session | Monday | Tuesday |
--------------------------------------------------------------------
"description" | "ClassCode" - "Room" | .... |
"from_time" - "to_time" or | "ClassDescription" | .... |
"StartTime" - "EndTime" | "StaffTitle" "StaffSurname" | .... |
---------------------------|-----------------------------|---------|
"description" | "ClassCode" - "Room" | .... |
"from_time" - "to_time" | "ClassDescription" | .... |
"StartTime" - "EndTime" | "StaffTitle" "StaffSurname" | .... |
| (if timetable #A exists then| |
| add below) | |
| ------------------- | |
| "ClassCode" - "Room" | |
| "ClassDescription" | |
| "StaffTitle" "StaffSurname" | |
---------------------------|-----------------------------|---------|
******************* EDIT 2***********************************
{
"description": "Recess",
"from_time": "10:25",
"to_time": "10:45",
"timetable": {
"1": [],
"2": [],
"3": [],
"4": [],
"5": []
}
},
****************** EDIT 3 ******************************************
{
"description": "Period 3",
"from_time": "10:45",
"to_time": "11:35",
"timetable": {
"1": {
"DayNumber": "1",
"ClassCode": "2ENS",
"ClassDescription": "English",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "10:45",
"EndTime": "11:35"
},
"2": {
"DayNumber": "2",
"ClassCode": "2ARS",
"ClassDescription": "Art",
"Room": "AR.1.10",
"StaffNameExternal": "Betty Smith",
"StaffTitle": "Ms",
"StaffSurname": "Smith",
"StartTime": "10:45",
"EndTime": "11:35"
},
"3": {
"DayNumber": "3",
"ClassCode": "2PES",
"ClassDescription": "Physical Education",
"Room": "SP.1.1",
"StaffNameExternal": "Anne Doe",
"StaffTitle": "Mrs",
"StaffSurname": "Doe",
"StartTime": "10:45",
"EndTime": "11:35"
},
"4": {
"DayNumber": "4",
"ClassCode": "2MAS",
"ClassDescription": "Mathematics",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "10:45",
"EndTime": "11:35"
},
"5": {
"DayNumber": "5",
"ClassCode": "2LBS",
"ClassDescription": "Library",
"Room": "",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "10:45",
"EndTime": "11:35"
},
"5A": [
{
"DayNumber": "5",
"ClassCode": "2LBS2",
"ClassDescription": "Library",
"Room": "RC.1.2",
"StaffNameExternal": "Rebecca Williams",
"StaffTitle": "Mrs",
"StaffSurname": "Gallagher",
"StartTime": "10:45",
"EndTime": "11:35"
}
]
}
},
Upvotes: 0
Views: 369
Reputation: 533
First make sure you JSON validates and simplify it too.
{
"data": [{
"description": "Home Room",
"from_time": "08:30",
"to_time": "08:45",
"timetable": [
{
"DayNumber": "1",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
{
"DayNumber": "2",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
{
"DayNumber": "3",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
{
"DayNumber": "4",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "08:45"
},
{
"DayNumber": "5",
"ClassCode": "2HRS",
"ClassDescription": "Homeroom",
"Room": "JS.1.6",
"StaffNameExternal": "Sally Jones",
"StaffTitle": "Ms",
"StaffSurname": "Jones",
"StartTime": "08:30",
"EndTime": "09:35"
}
]
}]
}
Then you can do something like this
<?php
$get_data = file_get_contents('inf.json');
$response = json_decode($get_data, true);
$rooms = $response['data']['0']['timetable'];
?>
<?php foreach ($rooms as $v) {
echo $v['DayNumber'];
echo " ";
echo $v['ClassCode'];
echo " ";
echo $v['ClassDescription'];
echo $v['Room'];
echo " ";
echo $v['StaffNameExternal'];
echo " ";
echo $v['StaffTitle'];
echo "<br>";
}
?>
Upvotes: 0
Reputation: 3218
$response = json_decode($get_data, true);
<table>
<thead>
<tr>
<th>Session</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
</tr>
</thead>
<tbody>
<?php foreach ($response['data'] as $data): ?>
<tr>
<td>
<?php echo $data['description']; ?><br/>
<?php echo $data['from_time'] . ' - ' . $data['to_time']; ?>
</td>
<?php foreach ($data['timetable'] as $desc): ?>
<?php if ($data['description'] != "Recess"): ?>
<td>
<?php echo $desc['ClassCode'] . ' - ' . $desc['Room']; ?><br/>
<?php echo $desc['ClassDescription']; ?><br/>
<?php echo $desc['StaffTitle'] . ' - ' . $desc['StaffSurname']; ?>
</td>
<?php else: ?>
<td></td>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
This code is assuming that the json value has complete timetable from Monday to Friday and the day is sort ascending.
Upvotes: 2