0

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"
                }
            ]
        }
    },
2
  • 1
    Hi, can you please give us an example of how you'd like the end result to be? I don't mean values but keys. Commented Jul 27, 2018 at 3:05
  • 1
    Hi Keith. I have updated the table with keys rather than values. I hope that helps. Thanks. Commented Jul 27, 2018 at 3:24

2 Answers 2

2
$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.

Sign up to request clarification or add additional context in comments.

13 Comments

That's brilliant! Thanks for that David. There is a couple of issues, however, and it's totally my fault because I didn't include the full JSON response.
are there any problems that you encountered?
Sorry, accidentally published my comment. I have added a second edit to my post for the first issue. After Period 2, there is "Recess" which does not contain the same keys/values as the other Periods.
The other issue is that some days have an extra period. See Edit 3. Period 3 has a "Day 5A". This is because students will have different subjects on alternating weeks.
I have updated the table in EDIT 1 to reflect the format to include "Day 5A" if it exists. I hope it makes sense
|
0

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>";
}
?>

1 Comment

you simply echo the value, not store the value in table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.