2017-11-05 3 views
0

보기 및 ClassID가에 결과를 표시하지하지만 난 배열을foreach 루프는 CodeIgniter는 내가 studentIDs으로 출석 주석의 여러 레코드를 얻을 시도하고

배열 ([0] => 배열 ([0] = 인쇄 할 때 > [stdClass Object ([attendanceID] => 90 [comment] => 이것은 자라 버트입니다.) [2] => 배열 ([0] => stdClass 객체 ([attendanceID] => 91 [comment] => 이것은 ibrahim입니다. ([참석자 ID] => 93 [코멘트] =>)) [5] => 배열 ([0] => 92) stdClass 객체 ([attendanceID] => 94 [comment] =>)) [6] => 배열 ([0] => stdClass 객체 ([attendanceID ([attendanceID] => 96 [comment] =>))))

하지만 인쇄 할 수 없습니다. .. 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(); 
} 
당신이 print_r의 출력을 볼 수 있듯이3210

보기

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

후 전체 컨트롤러 코드해야 내부 루프를 할 필요가있다. if (count ($ comments)) {} –

+0

대신에 if ($ comments) {}를 체크하여 귀하가 퀘스트에 언급 한 컨트롤러 전체를 추가하십시오. – Gagantous

+0

당신은 print_r을 사용하고 있습니까? 컨트롤러에서 어레이를보기 위해 전달하는 데 사용했던 방법은 무엇입니까? – Gagantous

답변

0

당신은 학생마다 여러 의견이있다. 그래서 당신은 당신이 데이터를 볼 수있는 배열을 전달하는 경우 다시

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

감사합니다. 하지만 내 새로운 문제를 참조 html td에서 반복되는 값 중첩 된 루프. [link] (https://stackoverflow.com/questions/47141765/nested-loop-repeating-value-in-html-td) –

0

<?php 

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


?>