在HTML td中重复嵌套的循环 [英] Nested loop repeating value in html td

查看:239
本文介绍了在HTML td中重复嵌套的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的结果显示

my result showing

但是我想

我的管理员

MY controller

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

我的模态

MY Modal

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

我想要匹配学生识别码& ClassID和显示相关的studentID的评论,但是在这个代码中你看到所有的评论重复同样的,可能是因为嵌套循环,但是不知道如何解决这个问题以及我做错了什么地方。 b $ b

I want match Student Id & ClassID and show related Comment of studentID, but in this code as you seeing all Comment Repeating same ,, might be because of nested loop but don't know how to fix this problem and where I'm doing wrong .

推荐答案

使用student_id创建注释数组。所以重复和冲突没有完成。

Create array of comment with use of student_id. So repeating and conflict not done.

在您的控制器 chnage您的评论为循环如下:

IN your controller chnage your comment for loop as below:

foreach ($studentID as $student) {
    $comments[$student] = $this->sattendance_m->get_comment($id,$student);  //<---add $student as index
 }

然后在 view 改变评论foreach如下

Then in your view change comment foreach as below

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 

      }
}

这篇关于在HTML td中重复嵌套的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆