使用动态列向HTML表添加行

时间:2014-02-19 05:11:58

标签: php

我正在使用下面的代码(作为Display mySQL records as HTML table columns上一个问题的解决方案提供)来生成一个HTML表格,如:

enter image description here

基于这些表:

周名称在表'周'中,每周记录还包含该周的会话数:

+---------+-----------+----------+-----------+
| 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 |
+-----------+-------------+-------------+-------------+ 

我现在想扩展它以将与会者显示为会话行下的附加表行,并指出他们参加的会话。出勤表是:

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

生成的HTML表格如下:

enter image description here

无论如何帮助修改下面的代码,以获得上述图片所获得的结果。

$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 个答案:

答案 0 :(得分:1)

以下代码可能是最简单的&amp;最接近你想要的。

  • 使用mysqli_*个函数。
  • 包含两个查询。
  • 第一个查询仅从cohortweek检索。
  • 在第一个while循环$weeksession数组中创建。它包含给定周数会话数的列号。

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

  • 如果一个人参加了2个或更多会话,那么它将以多行显示

答案 1 :(得分:1)

也许有帮助。

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