【问题标题】:Foreach loop not display result in Codeigniter ViewForeach 循环不在 Codeigniter 视图中显示结果
【发布时间】: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


【解决方案1】:

您可以看到print_r 的输出,每个学生有多个 cmets。所以你需要再做一次内循环

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

【讨论】:

  • 谢谢。但请参阅我的新问题 Nested loop repeating value in html td。 link
【解决方案2】:

这应该是

<?php

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


?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2018-10-10
    • 2021-07-24
    • 1970-01-01
    • 2015-07-13
    相关资源
    最近更新 更多