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