0

I need some help about PHP json. I am still newbie about this thing. I tried other ways but it seemed unsuccessful. I have this kind of json format:

{
        "schedule_backup": [
            {
                "class_time_id_fk": "1",
                "student_id_number": "AR0001",
                "teacher_id_number": "ACAD-0091",
                "class_type_id_fk": "1",
                "room_assignment_id_fk": "4",
                "books_materials_id_fk": "225",
                "class_level_id_fk": "23",
                "subject_id_fk": "57"
            },
            {
                "class_time_id_fk": "2",
                "student_id_number": "AR0001",
                "teacher_id_number": "ACAD-0049",
                "class_type_id_fk": "1",
                "room_assignment_id_fk": "41",
                "books_materials_id_fk": "211",
                "class_level_id_fk": "4",
                "subject_id_fk": "58"
            }
       ]
    }

And I want to display them on a table. Here is my code:

My View

echo $jsonString = $this->MClassSchedule->parse_schedule_backup();

$jsonArray = json_decode($jsonString,true);

echo $jsonArray['schedule_backup']['class_time_id_fk'];

Model

function parse_schedule_backup(){
    $this->db->select('*');
    $this->db->from('class_schedule_backup');
    $query = $this->db->get();

    foreach ($query->result() as $rows) {

        return $rows->data;

    }

}
4
  • Is there a particular reason why you are using Json for this? Will you be using Ajax or is this a straight to view thing? Commented Nov 22, 2017 at 11:26
  • Because you are returning and object which doesn't need to be Json decoded unless you want an array. In which case just return a result array and remove your foreach in the model. Then making a table is easy in the view with a foreach. Commented Nov 22, 2017 at 11:41
  • @Alex I just to view it. I want to use ajax later on. Commented Nov 22, 2017 at 11:43
  • @Alex Could you give an example for this? Commented Nov 22, 2017 at 11:44

1 Answer 1

1

Unless you are passing this asynchronously, or through an API, it seems messy to convert it to JSON then back... Just return the data array like in the model below.

Model

function parse_schedule_backup(){
    $this->db->select('*');
    $this->db->from('class_schedule_backup');
    $query = $this->db->get();

    // Return associative data array
    return $query->result_array();
}

Then in the view, just loop through the rows inside of a table structure. People have different styles for templating tables, but one way is below (obviously not including all columns).

My View

<?php
// You probably want to do this in the controller and pass the data to the view
$data = $this->MClassSchedule->parse_schedule_backup();
// Create table header
?>
<table>
   <thead>
       <tr>
           <th>Class ID</th>
           <th>Teacher ID</th>
           <th>Student</th>
       </tr>
   </thead>
   <tbody>
<?php
    // You probably want to check for a non-empty array and provide an error if needed
    foreach($data as $row){
        $json = json_decode($row['data'], TRUE);
?>
        <tr>
            <td><?= $json['schedule_backup']['timeID_1']['class_time_id_fk'] ?></td>
            <td><?= $json['schedule_backup']['timeID_1']['student_id_number'] ?></td>
            <td><?= $json['schedule_backup']['timeID_1']['teacher_id_number'] ?></td>
        </tr>
<?php 
    }
?>
   </tbody>
</table>
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your answer. But I got only the student_id_number the rest is empty.
Are there data for those other fields in your result? One thing you could do for testing is print the entire row in the foreach with say print_r to make sure you're getting the values you expect.
No. Only the student_id_number. In my database table I have ID, student_id_number, data and date_created. Where the json is stored in data row.
I'm confused now. Where were you getting the JSON object from if not the query provided? Or is the data column a JSON string?
Yes, the data column has a JSON string.
|

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.