douboshan1466 2014-02-19 05:11
浏览 14
已采纳

使用动态列向HTML表添加行

I'm using the code below (provided as a solution to a previous question at Display mySQL records as HTML table columns) to generate an HTML table like:

enter image description here

Based on these tables:

The week names are in table 'week' and each week record also contains the number of sessions for that week:

+---------+-----------+----------+-----------+
| week_pk | week_name | sessions | cohort_fk |
+---------+-----------+----------+-----------+
|       1 | Week 1    |        3 |         1 |
|       2 | Week 2    |        2 |         1 |
|       3 | Week 3    |        1 |         1 |
+---------+-----------+----------+-----------+

+-----------+-------------+-------------+-------------+
| cohort_pk | cohort_name | cohort_code | cohort_year |
+-----------+-------------+-------------+-------------+
|         1 | Some name   | MICR8976    |        2014 |
+-----------+-------------+-------------+-------------+ 

I now want to extend this to show attendees as additional table rows under the session row and indicate which session they attended. The attendance table is:

+---------------+-------------+---------+-----------+---------+---------+
| attendance_pk | given_names | surname | cohort_fk | week_fk | session |
+---------------+-------------+---------+-----------+---------+---------+
|             1 | Bill        | Smith   |       1   |       2 |       2 |
|             2 | Fred        | Jones   |       1   |       1 |       1 |
+---------------+-------------+---------+-----------+---------+---------+

The resulting HTML table would be like:

enter image description here

Anyway help on modifying the code below to get results as per the above image appreciated.

$cohort = '1';
$year = '2014';

    $query = "SELECT * FROM cohort, week, attendance 
    WHERE week.cohort_fk = cohort.cohort_pk 
    AND attendance.week_fk = week.week_pk
    AND attendance.cohort_fk = cohort.cohort_pk
    AND cohort.cohort_year = '$year' 
    AND cohort.cohort_pk = '$cohort'";

    $result = mysql_query($query, $connection) or die(mysql_error());

    echo "<table border='1'>";
    echo "<tr><td>Name</td>";
    $second_row = "<tr><td>Session</td>";
    while($row = mysql_fetch_assoc($result)){
        $weekname  = $row["week_name"];
        $n_session = $row["sessions"];
        echo "<td colspan='$n_session'>$weekname</td>";
        for($i=1; $i<=$n_session; $i++){
            $second_row .= "<td>S$i</td>";
        }
    }
    echo "</tr>";
    echo "$second_row</tr>";
    echo "</table>";
    ?>
  • 写回答

2条回答 默认 最新

  • dsk88199 2014-02-19 08:11
    关注

    The following code might be the simplest & nearest to what you want.

    • mysqli_* functions are used.
    • Two queries are included.
    • The first query retrieves only from cohort and week.
    • In the first while loop $weeksession array is created. It holds the column number for given week number and session number.

    $year = 2014;
    $cohort = 1;
    
    $query = "SELECT * FROM cohort, week 
    WHERE week.cohort_fk = cohort.cohort_pk 
    AND cohort.cohort_year = '$year' 
    AND cohort.cohort_pk = '$cohort'
    ORDER BY week.week_pk";
    
    $dblink = mysqli_connect("localhost", "root", "", "test");
    $result = mysqli_query($dblink, $query);
    
    echo "<table border='1'>";
    echo "<tr><td>Name</td>";
    $second_row = "<tr><td>Session</td>";
    $totalcolumn = 1;                               
    while( $row = mysqli_fetch_assoc($result) ){
        $weekname   = $row["week_name"];
        $n_session  = $row["sessions"];
        $weekpk     = $row["week_pk"];              
        $totalcolumn += $n_session;                 
        echo "<td colspan='$n_session'>$weekname</td>";
        for($i=1; $i<=$n_session; $i++){
            $second_row .= "<td>S$i</td>";
            $weeksession[$weekpk][$i] = $totalcolumn - $n_session + $i;
        }
    }//end while
    echo "</tr>";
    echo $second_row . "</tr>";
    
    
    $query = "SELECT * FROM cohort, week, attendance 
    WHERE week.cohort_fk = cohort.cohort_pk 
    AND attendance.week_fk = week.week_pk
    AND attendance.cohort_fk = cohort.cohort_pk
    AND cohort.cohort_year = '$year' 
    AND cohort.cohort_pk = '$cohort'
    ORDER BY attendance.attendance_pk";
    $result = mysqli_query($dblink, $query);
    while( $row = mysqli_fetch_assoc($result) ){
        $name   = $row["given_names"] . " " . $row["surname"];
        $weekpk     = $row["week_pk"];
        $sno        = $row["session"];
        echo "<tr><td>$name</td>";
        for($i=2; $i<=$totalcolumn; $i++){      
            if( $weeksession[$weekpk][$sno] == $i )
                echo "<td>X</td>";
            else
                echo "<td>-</td>";              
        }                                       
        echo "</tr>";
    }//end while
    echo "</table>";
    

    • If a person attends 2 or more sessions, then it will be shown in multiple rows
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 html5+css有人可以帮吗?
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?