【发布时间】:2017-11-05 12:06:51
【问题描述】:
我正在尝试通过 studentID 和 ClassID 获取多条出勤评论记录,但是当我打印 Array 时
Array ( [0] => Array ( [0] => stdClass Object ( [attendanceID] => 89 [comment] => 这是 Yusra ) ) [1] => Array ( [0] => stdClass Object ( [attendanceID] => 90 [comment] => 这是 zara butt ) ) [2] => 数组 ( [0] => stdClass Object ( [attendanceID] => 91 [comment] => 这是 ibrahim ) ) [ 3] => Array ([0] => stdClass Object ([attendanceID] => 92 [comment] => comment)) [4] => Array ([0] => stdClass Object ([attendanceID] => 93 [ comment] => ) ) [5] => Array ( [0] => stdClass Object ( [attendanceID] => 94 [comment] => ) ) [6] => Array ( [0] => stdClass Object ( [出勤ID] => 95 [comment] => ) ) [7] => 数组 ([0] => stdClass 对象 ([attendanceID] => 96 [comment] => ) ) )
但我使用 foreach 循环在视图中打印没有显示.. 不知道我在哪里做错了
控制器
$studentID = []; // Multiple Student ids
$comments = [];
$this->data['set'] = $id; //class id
$this->data['students'] = $this->student_m->get_order_by_student(array('classesID' => $id, 'schoolyearID' => $schoolyearID));
foreach($this->data['students'] as $key => $val){
$studentID[] = $val->studentID; //
}
foreach ($studentID as $student) {
$comments[] = $this->sattendance_m->get_comment($id,$student);
}
$this->data['comments']= $comments;
print_r($comments);
型号
function get_comment($id,$student) {
$this->db->select('attendanceID,comment');
$this->db->where('classesID', $id);
$this->db->where_in('studentID', $student);
$this->db->order_by($this->_primary_key,"desc");
$this->db->limit(1);
$this->db->from($this->_table_name);
$query=$this->db->get();
return $query->result();
}
查看
<?php
if(count($comments)) {
foreach ($comments as $key => $row) {
echo $row->comment;
}
}?>
【问题讨论】:
-
发布整个控制器代码,您将数据数组传递给视图。只需检查 if($cmets) {} 而不是 if(count($cmets)) {}
-
添加您在任务中提到的整个控制器。请
-
您使用的是 print_r,您使用哪种方法将数组从控制器传递给查看?
标签: php arrays codeigniter foreach