Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

my result showing enter image description here

but i Want enter image description here

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 .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 

      }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...