简体   繁体   English

使用动态列向HTML表添加行

[英]Adding rows to a HTML table with dynamic columns

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: 我正在使用下面的代码( 作为HTML表格列显示mySQL记录中的上一个问题的解决方案)生成HTML表格,如:

在此输入图像描述

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: 生成的HTML表格如下:

在此输入图像描述

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>";
    ?>

The following code might be the simplest & nearest to what you want. 以下代码可能是最简单且最接近您想要的代码。

  • mysqli_* functions are used. mysqli_*函数。
  • Two queries are included. 包括两个查询。
  • The first query retrieves only from cohort and week . 第一个查询仅从同cohortweek检索。
  • In the first while loop $weeksession array is created. 在第一个while循环中创建$weeksession数组。 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 如果一个人参加2个或更多会话,那么它将以多行显示

maybe it helps. 也许有帮助。

<?php

$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'";
$result = mysql_query($query, $connection) or die(mysql_error());

$rows = array();
$weeks = array();
$sessions = array();
while ($rows[] = $row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $weeks[] = "<td colspan='" . (int) $row['sessions'] . "'>" . $row['week_name'] . "</td>";
    for ($i = 1; $i <= (int) $row['sessions']; $i++) {
        $sessions[] = "<td>S" . $i . "</td>";
    }
}

$attendance = array();
$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 = mysql_query($query, $connection) or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $attendance[] = "<tr><th>" . $row['given_names'] . " " . $row['surname'] . "</th>";
    foreach ($rows as $week) {
        for ($i = 1; $i <= (int) $week['sessions']; $i++) {
            if ($row['week_fk'] == $week['week_pk'] && $row['session'] == $i) {
                $attendance[] = "<td>X</td>";
            } else {
                $attendance[] = "<td></td>";
            }
        }
    }
    $attendance[] = "</tr>";
};

$table = array(
    "<table width = 50% border = '1' cellspacing = '2' cellpadding = '0'>",
    "<tr><th>Name</th>", join('', $weeks), "</tr>",
    "<tr><th>Sessions</th>", join('', $sessions), "</tr>",
    join('', $attendance),
    "</table>"
);

echo join("", $table);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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