Я использую приведенный ниже код (предоставляемый в качестве решения предыдущего вопроса в Display mySQL records в виде столбцов таблицы HTML ) для создания таблицы HTML, например:
На основе этих таблиц:
Имена недель указаны в таблице «неделя», и каждая неделя записи также содержит количество сеансов на этой неделе:
+---------+-----------+----------+-----------+ | 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 будет выглядеть так:
В любом случае, помогите изменить код ниже, чтобы получить результаты в соответствии с приведенным выше изображением.
$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>"; ?>
Следующий код может быть самым простым и ближайшим к тому, что вы хотите.
mysqli_*
функции mysqli_*
. cohort
и week
. $weeksession
цикл $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>";
возможно, это помогает.
<?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);