PHP循环数组直到索引更改

How can I create a php loop that loops over until the index changes? For example, if the index is Student, and the data column is all the classes the student is enrolled for, how can I display each class next to the current student, then move on to the next student and display all the classes of that student and so on? I am completely stuck and I just need an idea to help get started. I believe i would need a foreach or while loop, that says while student_id is not changed, display class. Or something along those lines, I've been stuck here for days. I also have an element that displays data for each student in a one to many fashion, however, since this is a many to many table, i created an entire separate table for the classes of each student.

$get_student = "select * from students";

    $run_student = mysqli_query($Conn, $get_student);

    while($row_product=mysqli_fetch_array($run_student)){

        $student_id = $row_product['ID'];
        $student_name = $row_product['Name'];
        $student_code = $row_product['Code'];
        $student_image = $row_product['Image'];


        echo "
    <div class='' id='' >$student_id $student_name $student_code</div>
<br><br><br><br>
";}

Basically, I would like to call from the second table, the one with the courses, and list them within the div with the other information. This table looks as follows

ClassTable
ID-------Class
1--------101
1--------131
2--------555
3--------225
3--------146
3--------269

I really need help on how to get started here, any help on where to start would be very helpful.

EDIT: Okay, so for the first table (one to many), the structure is

ID(PK)
Name(index)
Code
Image 

The second (Many to Many) structure is:

TABLEID(PK)
Student_ID(index)
Student_Name(index)
Course

I made a relationship in the phpmyadmin designer between ID and Student_ID and as well as Name and Student_Name.

I hope this clarifies the structure.

EDIT 2

As suggested, I made the edits to the above php code:

    /*$get_student = "select * from inventory";*/
        $get_student = "SELECT s.ID, S.Code, s.Name,
(
SELECT
  GROUP_CONCAT(c.Course SEPARATOR ', ')
from
  mydatabase.coursetable c
where
  c.Student_ID = s.ID
) as courses
FROM mydatabase.studenttable s" ;

However, I get the following error

 mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp64\www\...

i'd recommend you to improve your query, something like this.

$query_string = 
"SELECT s.ID, S.Code, s.Name,
(
SELECT
  GROUP_CONCAT(c.Course SEPARATOR ', ')
from
  yourdatabase.coursetable c
where
  c.Student_ID = s.ID
) as courses
FROM yourdatabase.studenttable s
ORDER BY s.ID ASC 
";
//Added ORDER BY to order the students from first id to last.

$actual_query = mysqli_query($conn,$query_string);

while($result = mysqli_fetch_assoc($actual_query)){
//do stuff with the result array
}

You should end up having array like:

$result
[ID] => (1)

[Code] => (XY35XS)

[Name] => (John Doe)

[Courses] => (101, 102, 103)

So now you could just loop it through foreach or a while like you already did.

Edited my suggestion a little bit to include the mysqli parts.

I dont exactly get your question but try if this works, first get list of students from table as

 $sql = "select id,Name from students";  
 $students = mysqli_query($Conn, $sql);

Now for each student you need the course right? so

 foreach($students as $student){
    echo $student[Name];
    $sql = "select course from courses where Student_id = ".$student[id];
    $studentCourse = mysqli_query($Conn, $sql);

    foreach($studentCourse as $std)
        {
         echo $std[course]; 
    }
 } 

Hope this works.