我的 rowspan 有问题,它将数据库中的所有数据打印到一行中!即使ID不同的那个!输出应该是课程名称和参加该课程的所有学生,把我的代码只取数据库中的第一个课程名称并将其与数据库中的所有学生姓名一起打印出来!
我的代码是
<h2> List of course Name with students names</h2>
<?php
include('../connect.php');
$id=$_SESSION['login_user'];
$sql = "SELECT C.CourseName ,GROUP_CONCAT(s.Studntname) AS Studntname
FROM course AS c
INNER JOIN student AS s
ON s.CourseID = c.CourseID";
$result = mysql_query ($sql, $connection);
echo "<center>";
echo "<table>";
echo "<tr> <th>Course Name</th> <th> Student Name</th> </tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['CourseName'] . '</td>';
echo "<td rowspan='' >" .$row['Studentname'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</center>";
?>
我的两张表是:
课程
CourseName var(30)
CourseID int(7)
学生
Studentname var(30)
StudentID int(7)
CourseID int(7)