简体   繁体   中英

Foreach loop not display result in Codeigniter View

I'm trying get multiple records of attendance comment by studentIDs, and ClassID but when i print Array

Array ( [0] => Array ( [0] => stdClass Object ( [attendanceID] => 89 [comment] => This is Yusra ) ) [1] => Array ( [0] => stdClass Object ( [attendanceID] => 90 [comment] => this is zara butt ) ) [2] => Array ( [0] => stdClass Object ( [attendanceID] => 91 [comment] => this is 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 ( [attendanceID] => 95 [comment] => ) ) [7] => Array ( [0] => stdClass Object ( [attendanceID] => 96 [comment] => ) ) )

but i print in view using foreach loop nothing show.. don't know where i'm doing wrong

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

Model

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

View

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

As you can see output of print_r you have multiple comments per student. So you need to do inner loop again

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

this should be

<?php

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


?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM