Foreign循环不显示Codeigniter视图

时间:2017-11-05 12:06:52

标签: php arrays codeigniter foreach

我尝试通过studentID和ClassID获取多个出勤评论记录,但是当我打印数组时

  

数组([0] =>数组([0] => stdClass对象([attendanceID] => 89 [评论] =>这是Yusra))[1] =>数组([0 ] => stdClass对象([attendanceID] => 90 [comment] =>这是zara butt))[2] =>数组([0] => stdClass对象([attendanceID] => 91 [comment] =>这是ibrahim))[3] =>数组([0] => stdClass对象([attendanceID] => 92 [评论] =>评论))[4] =>数组([0] => stdClass对象([attendanceID] => 93 [评论] =>))[5] =>数组([0] => stdClass对象([attendanceID] => 94 [comment] =>))[6] =>数组([0] => stdClass对象([attendanceID] => 95 [评论] =>))[7] =>数组([0 ] => stdClass对象([attendanceID] => 96 [评论] =>)))

但我使用foreach循环在视图中打印没有显示..不知道我在哪里做错了

Controllor

$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;
    } 
}?>

2 个答案:

答案 0 :(得分:0)

正如您可以看到print_r的输出,每位学生都有多条评论。所以你需要再次进行内循环

if(count($comments)) 
{
  foreach ($comments as $key => $row) {
     foreach ($row as  $value) {
       echo $value->comment;
     }
  } 
}

答案 1 :(得分:0)

这应该是

<?php

$i=0;
if($comments)
{
  foreach ($comments as $key => $row) {
    echo $key[$i++]->row
  } 
}else{
echo "null";
}


?>