johnlopev
johnlopev

Reputation: 97

How to display data of json using codeigniter

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;

    }

}

Upvotes: 0

Views: 3352

Answers (1)

kchason
kchason

Reputation: 2885

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>

Upvotes: 1

Related Questions