[英]How to display all the data from database using json into view page in Codeigniter framework
[英]How to display data of json using codeigniter
我需要有关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;
}
}
除非您通过异步方式或通过API传递此参数,否则将其转换为JSON然后再返回似乎很麻烦……只需像下面的模型中那样返回数据数组即可。
模型
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.