MY控制器
if((int)$id) {
$studentID = [];
$comments = [];
$this->data['set'] = $id;
$schoolyearID = $this->session->userdata('defaultschoolyearID');
$this->data['classes'] = $this->student_m->get_classes();
$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;
$this->load->view('_layout_main', $this->data);
我的模态
function get_comment($id,$student) {
$this->db->select('*');
$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 foreach($students as $student) { ?>
<tr>
<td data-title="<?=$this->lang->line('attendance_name')?>">
<?php echo $student->name; ?>
</td>
<td data-title="<?=$this->lang->line('attendance_roll')?>">
<?php echo $student->roll; ?>
</td>
<td data-title="<?=$this->lang->line('attendance_comment')?>">
<?php
if(count($comments))
{
$attendanceID = [];
$student_ID = [];
$comment = [];
$classid = [];
foreach ($comments as $key => $row) {
foreach ($row as $value) {
$attendanceID [] = $value->attendanceID;
$student_ID [] = $value->studentID;
$classid [] = $value->classesID;
//$comments [] = $value->comment;
$key = array_search($student->studentID,$student_ID); //find same StudentID ids
$key2 = array_search($set,$classid); // find same classID
if($student_ID[$key] == $student->studentID && $set == $classid[$key2]){
?>
<?php
echo $value->comment; // Showing Comment where Both IDs same
?>
<?php }
}
}
}
?>
</td>
<?php } ?>
</tr>
<?php } ?>
我想要匹配学生ID&amp; ClassID并显示相关的studentID注释,但在此代码中,当您看到所有注释重复相同时,可能是因为嵌套循环但不知道如何解决此问题以及我在哪里做错了。
答案 0 :(得分:0)
使用student_id创建评论数组。所以重复和冲突没有完成。
在您的控制器中按照以下说明循环注释:
foreach ($studentID as $student) {
$comments[$student] = $this->sattendance_m->get_comment($id,$student); //<---add $student as index
}
然后在视图中更改评论foreach,如下所示
foreach ($comment[$student->studentID] as $key => $row) {
foreach ($row as $value) {
$attendanceID [] = $value->attendanceID;
$student_ID [] = $value->studentID;
$classid [] = $value->classesID;
echo $value->comment; // Showing Comment
}
}