简体   繁体   中英

Get records from database in table with rowspan

I've try a lot of ways to get this table print as good as it should but I failed. I know it's simple thing so I hope you help me with it.
Here's my code:

<?php
    include('../connect.php');
    $id=$_SESSION['login_user'];
    $sql = "Select CourseName , Studentname from course p natural join student t"; 
    $rs_result = mysql_query ($sql, $connection); 

    echo "<center>";
    echo "<table>";
    echo "<tr> <th>Course Name</th> <th> Students Name</th>  </tr>";

    // loop through results of database query, displaying them in the table
    while($row = mysql_fetch_array( $rs_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>";
?>

I want to be something like this

Course   |  Name   |   Student name  |
Math101  |  john, Mike               |
...

Also, is the JOIN query between the two tables CORRECT or not?

The two tables are:
Course ( Course name - Course id )
Student ( Student name - Course id )

Try this query

$sql ="SELECT cor.CourseName,GROUP_CONCAT(stu.StudentName) AS StudentName
        FROM course AS cor
        LEFT JOIN student AS stu
           ON stu.CourseId = cor.CourseId";

And change the the line in below

echo "<td rowspan=''>" . $row['Studentname'] . "</td> ";

This line:

 echo "<td rowspan=''> $row[Studentname] </td> ";

You are accessing the array element improperly. Studentname should have single quotes around it like such:

 echo "<td rowspan=''>" . $row['Studentname'] . "</td> ";

Also, in your query, this may work better:

$sql = "SELECT c.CourseName, s.StudentName
        FROM course AS c
        INNER JOIN student AS s
           ON s.CourseId = c.CourseId";

Please use below format

SELECT CourseName , Studentname 
    FROM course
    INNER JOIN student
    ON course.id = student.id

Thanks

The problem is with Your rowspan attribute - You need to provide it with the exact number of rows to span through. Anyway, I think it is the collspan attribute You want to use, so eg

echo "<td collspan='2'> {$row['Studentname']} </td> ";

which means it will span through 2 columns, thus stundet's name will be both under the Name and Student name columns.

Is this what You were expecting?

Also I highly recommend not to use mysql_ functions but learn how to use mysqli or at least PDO .

I'm under the impression that you wanted to display a comma-separated list of names of all the students that attend each course, for each separate CourseName. In this case, you could change your SQL query to something like this:

SELECT CourseName, GROUP_CONCAT(Studentname SEPARATOR ', ') as names
FROM Course p NATURAL JOIN Student t
GROUP BY CourseName;

Hello please take a look of this answer. Dynamic rowspan while fetching records from database

I think it might be helpfull.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM