Ik zou het waarschijnlijk zo coderen, waarschijnlijk niet de enige manier, maar zou moeten werken op basis van wat ik denk dat je probeert te bereiken. Zorg ervoor dat u ook "bestellen op" toevoegt aan uw SQL-query.
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>";