每次乘法的SQL行

Using on this table

 [         POSTS         ]
  | id |   title  | class |
  |----|----------|-------|
  | 1  |   4567   |    2  |
  | 2  |   1234   |    1  |
  | 3  |   9124   |    1  |
  | 3  |   9124   |    3  |
  ________________________

This code

    $statment= $conn->prepare("SELECT * FROM Posts WHERE id = :id AND class in (1,2,3) ORDER BY class ASC");
$statment->execute([':id' => $id]);


  $classes = array();
  while($row = $statment->fetch()){
  if(!isset($classes[$row['class']])){ $classes[$row['class']] = array(); }
  $classes[$row['class']][] = $row['title'];
}


    foreach($classes as $key => $class){
   echo "Class Value: ".$key;
   foreach($class as $title){ echo "<a>".$title."</a>"; }

}

When i put the foreach code, Every value gets the $key, But the echo "Class Value: ".$key; is supposed to appear once, Then all the values that has $key appears and it, But instead the $key is multiplied & When the classes that has for example class = 1, is more than 3, Everything starts to break.

Supposed to be

$key:
ROW VALUE 1
ROW VALUE 1
ROW VALUE 1

What happens

$key:
ROW VALUE 1 [id = 1]
$key:
ROW VALUE 1 [id = 2]
$key:
ROW VALUE 1 [id = 3]

When there is more than 3 rows with value 1

$key:
ROW VALUE 1 [id = 1]
$key:
ROW VALUE 1 [id = 1]
$key:
ROW VALUE 1 [id = 2]
$key:
ROW VALUE 1 [id = 3]

This code was an answer for a previous question i asked here https://stackoverflow.com/a/43054136/7490349