Nested loop repeating value in html td

2019-08-25 04:11发布

问题:

my result showing

but i Want

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

My VIEW

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

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 .

回答1:

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

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
 }

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 

      }
}