Saya mungkin akan mengkodekannya seperti ini, mungkin bukan satu-satunya cara tetapi harus bekerja berdasarkan apa yang saya yakini ingin Anda capai. Pastikan untuk menambahkan "pesan berdasarkan" ke kueri SQL Anda juga.
echo "<table cellspacing='40'>";
$query2 = "SELECT mods.mid, ayr, mtitle, credits FROM stud, smod, mods WHERE stud.sid = '".$_POST['stuNo']."' and stud.sid = smod.sid and smod.mid = mods.mid ORDER BY ayr DESC";
$result = mysql_query($query2) or die(mysql_error());
echo "Enrollment and Progress";
echo "<table width='150' border='1'>";
echo "<tr><td colspan="3"><b>Module Selection</b></td></tr>";
$year = "";
while ($row = mysql_fetch_array($result)) {
if ($year != $row["ayr"]) {
echo "<tr><td colspan="3"><b>" . $row["ayr"] . "</b></td></tr>";
$year = $row["ayr"];
}
echo "<tr>";
echo "<td>" . $row["mid"] . "</td>";
echo "<td>" . $row["mtitle"] . "</td>";
echo "<td>" . $row["credits"] . "</td>";
echo "<tr>";
}
echo "</table>";