[英]Single HTML Table from multiple MySQL tables
我一直在努力爭取這個; 我會盡可能簡單地在這里解釋一下。
考慮這個MySQL表:
+----------+-----------+---------+--------+
|status_id |session_id |pilot_id |present |
+----------+-----------+---------+--------+
|1 |61 |901 |1 |
|2 |63 |901 |1 |
|3 |62 |901 |0 |
|4 |62 |902 |1 |
|5 |63 |903 |1 |
+----------+-----------+---------+--------+
session_id
和pilot_id
都是外鍵,引用另一個表中的主鍵。 相同的pilot_id
可以與不同的session_id
相關聯,但是每個pilot_id
- session_id
組合都是唯一的。
我需要創建一個HTML表格(在PHP中),它將顯示如下數據:
+----------+---------+---------+---------+
| |61 |62 |63 |
+----------+---------+---------+---------+
|901 |X | |X |
|902 | |X | |
|903 | | |X |
+----------+---------+---------+---------+
因此,行是pilot_id
,列是session_id
。 當pilot_id
- session_id
組合的present
值為1時,應檢查相應的單元格。 (即,當行組合為零或MySQL表中不存在組合時,HTML表中不應出現任何內容)
唷。
有任何想法嗎?
謝謝!
我已經嘗試了erisco提出的答案,但我很困惑。 (評論字段對於我的解釋來說太小了,因此我的問題更新了)。
這是我正在使用的實際數據:
+----------+-----------+---------+--------+
|status_id |session_id |pilot_id |present |
+----------+-----------+---------+--------+
|7 |65 |33 |1 |
|8 |66 |33 |1 |
|9 |65 |17 |0 |
|10 |66 |16 |1 |
+----------+-----------+---------+--------+
我使用$rows = mysqli_fetch_array($result);
。 我已經確認查詢正在返回正確的數據。
但是,當我使用ericso提出的答案時,我看起來似乎是任意數據。 這是生成的HTML表:
+----------+---------+---------+---------+---------+
| |1 |3 |6 |7 |
+----------+---------+---------+---------+---------+
|1 |X | | | |
|3 | | | | |
|6 | | | | |
|7 | | | | |
+----------+---------+---------+---------+---------+
此外,'X'位置與present
值保持不變。
任何想法為什么會這樣?
謝謝!
幸運的是,您只需要一個查詢。 假定$ rows是從數據庫中提取的數據的格式:
<?php
$rows = array(
array(
'status_id' => 1,
'session_id' => 61,
'pilot_id' => 901,
'present' => 1,
),
array(
'status_id' => 2,
'session_id' => 63,
'pilot_id' => 901,
'present' => 1,
),
array(
'status_id' => 3,
'session_id' => 62,
'pilot_id' => 901,
'present' => 0,
),
array(
'status_id' => 4,
'session_id' => 62,
'pilot_id' => 902,
'present' => 1,
),
array(
'status_id' => 5,
'session_id' => 63,
'pilot_id' => 903,
'present' => 1,
)
);
$session_ids = array();
$pilot_ids = array();
$crosses = array();
foreach ($rows as $row) {
$session_ids[$row['session_id']] = $row['session_id'];
$pilot_ids[$row['pilot_id']] = $row['pilot_id'];
if ($row['present'] == 1) {
$cross_index = $row['session_id'].'.'.$row['pilot_id'];
$crosses[$cross_index] = $cross_index;
}
}
sort($session_ids);
sort($pilot_ids);
?>
<table>
<tr>
<th></th>
<?php foreach ($session_ids as $sess_id): ?>
<th><?php echo $sess_id; ?></th>
<?php endforeach; ?>
</tr>
<?php foreach ($pilot_ids as $pilot_id): ?>
<tr>
<th><?php echo $pilot_id; ?></th>
<?php foreach ($session_ids as $sess_id): ?>
<?php if (isset($crosses[$sess_id.'.'.$pilot_id])): ?>
<td>X</td>
<?php else: ?>
<td></td>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
你可以使用這樣的算法:
$sql = "SELECT DISTINCT session_id AS sid FROM pilot_session ORDER BY 1 ASC";
$rs = mysql_query($sql, $conn);
$sessions = array();
while(false !== ($r = mysql_fetch_array($rs))){
$sessions[] = $r['sid'];
}
$sql = "SELECT DISTINCT pilot_id AS pid FROM pilot_session ORDER BY 1 ASC";
$rs = mysql_query($sql, $conn);
$pilots = array();
while(false !== ($r = mysql_fetch_array($rs))){
$pilots[] = $r['pid'];
}
$pilot_presence = array();
$sql = "SELECT session_id, pilot_id, present FROM pilot_session";
$rs = mysql_query($sql, $conn);
while(false !== ($r = mysql_fetch_array($rs))){
$s_presence[$r['pilot_id']][$r['session_id']] = $r['present'];
}
echo "<table><tr><td> </td>";
foreach($sessions as $s){
echo "<td>$s</td>";
}
echo "</tr>";
foreach($pilots as $p){
echo "<tr><td>$p</td>";
foreach($sessions as $s){
$tp = '';
if(isset($s_presence[$p][$s])){
if($s_presence[$p][$s] == '1'){
$tp = 'X';
}
}
echo "<td>".$tp."</td>";
};
echo "</tr>";
}
echo "</table>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.