Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用codeigniter显示json的数据_Php_Json_Codeigniter - Fatal编程技术网

Php 如何使用codeigniter显示json的数据

Php 如何使用codeigniter显示json的数据,php,json,codeigniter,Php,Json,Codeigniter,我需要一些关于PHP json的帮助。我对这件事还是个新手。我试过其他方法,但似乎不成功。我有这种json格式: { "schedule_backup": [ { "class_time_id_fk": "1", "student_id_number": "AR0001", "teacher_id_number": "ACAD-0091",

我需要一些关于PHP json的帮助。我对这件事还是个新手。我试过其他方法,但似乎不成功。我有这种json格式:

{
        "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"
            }
       ]
    }
我想把它们展示在桌子上。这是我的密码:

我的观点

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

$jsonArray = json_decode($jsonString,true);

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

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;

    }

}
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();
}

除非您以异步方式或通过API传递此消息,否则将其转换为JSON然后返回似乎很麻烦。。。只需像下面的模型一样返回数据数组

型号

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;

    }

}
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();
}
然后在视图中,只需循环表结构中的行。人们有不同的表格模板样式,但有一种方法如下(显然不包括所有列)

我的观点

<?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>

类ID
教师证
学生

使用Json进行此操作有什么特殊原因吗?您是使用Ajax还是直接查看对象?因为您返回的是不需要Json解码的对象,除非您需要数组。在这种情况下,只需返回一个结果数组并删除模型中的foreach。那么,用foreach在视图中制作桌子很容易。@Alex我只是想查看一下。我想稍后使用ajax。@Alex你能举个例子吗?谢谢你的回答。但是我只得到了学生id号,其余的都是空的。你的结果中有其他字段的数据吗?测试时可以做的一件事是使用say
print\r
打印
foreach
中的整行,以确保获得预期的值。否。只有学生证号码。在我的数据库表中,我有ID、学生ID、编号、数据和创建日期。json存储在数据行中的位置。我现在很困惑。如果没有提供查询,您从哪里获取JSON对象?或者
data
列是JSON字符串吗?是的,数据列有JSON字符串。