动态 HTML 表格 - 更新单元格行 [英] Dynamic HTML table - Updating cell rows
问题描述
我有一个从数据库动态生成的 HTML 表.实现这一点的解决方案可以在这个问题 Adding rows to带有动态列的 HTML 表格.
I have an HTML table being dynamically generated from database. The solution for achieving this can be seen at this question Adding rows to a HTML table with dynamic columns.
这很好用,除了我想在同一行中指出一个人每周参加的所有会议 - 使用下面的代码,额外的会议出席会成为附加到 HTML 表的另一行.所以我想要的是:
This works fine, except I want to indicate all sessions a person attended for each week in the same row - with the code below, an additional session attendance becomes another row appended to the HTML table. So what I want is like:
DB 表就像('week'、'cohort' 和 'attendance' 表)
DB tables are like ('week', 'cohort' and 'attendance' tables)
+---------+-----------+----------+-----------+
| 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 | person_id | given_names | surname | cohort_fk | week_fk | session |
+---------------+-----------+-------------+---------+-----------+---------+---------+
| 1 | 123456 | Bill | Smith | 1 | 1 | 2 |
| 2 | 123456 | Bill | Smith | 1 | 2 | 2 |
| 3 | 753354 | Fred | Jones | 1 | 1 | 1 |
| 4 | 753354 | Fred | Jones | 1 | 2 | 1 |
| 5 | 753354 | Fred | Jones | 1 | 3 | 1 |
+---------------+-----------+-------------+---------+-----------+---------+---------+
以及我正在使用的代码:
And the code that I'm using:
$cohort = $_POST['cohort'];
$year = $_POST['year'];
$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";
$result = mysql_query($query, $connection) or die(mysql_error());
echo "<table width='100%' cellpadding='4' cellspacing='0' class='attendance_table'>";
echo "<tr><td class='theadings'></td>";
$second_row = "<tr><td class='theadings'></td>";
$totalcolumn = 1;
while( $row = mysql_fetch_assoc($result) ){
$weekname = $row["week_name"];
$n_session = $row["sessions"];
$weekpk = $row["week_pk"];
$totalcolumn += $n_session;
echo "<td class='theadings' colspan='$n_session'>$weekname</td>";
for($i=1; $i<=$n_session; $i++){
$second_row .= "<td class='theadings_lab'>Lab $i</td>";
$weeksession[$weekpk][$i] = $totalcolumn - $n_session + $i;
}
}
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 = mysql_query($query, $connection) or die(mysql_error());
while( $row = mysql_fetch_assoc($result) ){
$name = $row["given_names"] . " " . $row["surname"];
$weekpk = $row["week_pk"];
$sno = $row["session"];
echo "<tr><td class='tborder_person_left'>$name</td>";
for($i=2; $i<=$totalcolumn; $i++){
if( $weeksession[$weekpk][$sno] == $i )
echo "<td class='tborder_person_attended'>✔</td>";
else
echo "<td class='tborder_person'>-</td>";
}
echo "</tr>";
}//end while
echo "</table>";
下面的@Kickstart 是表格与您的代码的示例.您可以看到例如 Melody Chew 和 Kit Yeng Melody Chew(同一个人)有两个单独的行.唯一标识符需要在出勤表中存在的 person_id 上(抱歉之前没有显示这个!我的坏还要注意表右侧的附加列,其中应在第 2 周列.
@Kickstart below is an example of what the table looks like with your code. You can see for example Melody Chew and Kit Yeng Melody Chew (same person) have two seperate rows. The unique identifier needs to be on the person_id which exists in the attendance table (apologies for not showing this before! my BAD Note also the additional columns on the right of the table with the crosses which should be under the week 2 column.
推荐答案
我个人会使用嵌套数组.
我给你举个例子.请注意,这只是原则上,因为我没有数据库,我没有测试.:)
I personally would go through nested arrays.
I give you an example. Note that this is only in principle, as I do not have the database, I have not tested. :)
1 -> 声明数组 $liste_name = array();
2 -> 将您的第二个治疗替换为:
1 -> statement of the array $liste_name = array();
2 -> Replace your second treatment by :
while( $row = mysql_fetch_assoc($result) ){
$name = $row["given_names"] . " " . $row["surname"];
$weekpk = $row["week_pk"];
$sno = $row["session"];
$person_id = $row["person_id"]; //add person_id
$liste_name[$person_id] = $name; // to have the last name
$tab[$person_id][1] = "<td class='tborder_person_left'>$name</td>"; // to have the last name
for($i=2; $i<=$totalcolumn; $i++){
if( $weeksession[$weekpk][$sno] == $i )
$tab[$person_id][$i] = "<td class='tborder_person_attended'>✔</td>";
}
}//end while
3 -> 填充空白区域:
3 -> Fill the empty areas :
foreach ($liste_name as $person_id => $name){
$tab2[$person_id][1] = $tab[$person_id][1];
for($i=2; $i<=$totalcolumn; $i++){
if (!isset($tab[$person_id][$i]) || ($tab[$person_id][$i] != "<td class='tborder_person_attended'>✔</td>"))
$tab[$person_id][$i] = "<td class='tborder_person'>-</td>";
$tab2[$person_id][$i] = $tab[$person_id][$i];
}
}
4 -> 使用数组:
foreach($tab2 as $person_id => $col){
echo "<tr>";
foreach ($col as $i => $value){
echo $value;
}
echo "</tr>";
}
echo "</table>";
我尝试使用虚构数据,结果奏效.:)
I try with fictional data and it works. :)
这篇关于动态 HTML 表格 - 更新单元格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!