来自多个MySQL表的单个HTML表

Dav*_*ard 6 php mysql database

我一直在努力争取这个; 我会尽可能简单地在这里解释一下.

考虑这个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       |
+----------+-----------+---------+--------+
Run Code Online (Sandbox Code Playgroud)

无论session_idpilot_id外键进行参照另一个表的主键.同样pilot_id可以用不同的关联session_id,但每pilot_id- session_id组合是唯一的.

我需要创建一个HTML表格(在PHP中),它将显示如下数据:

+----------+---------+---------+---------+
|          |61       |62       |63       |
+----------+---------+---------+---------+
|901       |X        |         |X        |
|902       |         |X        |         |
|903       |         |         |X        |
+----------+---------+---------+---------+
Run Code Online (Sandbox Code Playgroud)

因此,行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       |
+----------+-----------+---------+--------+
Run Code Online (Sandbox Code Playgroud)

我用$rows = mysqli_fetch_array($result);.我已经确认查询正在返回正确的数据.

但是,当我使用ericso提出的答案时,我看起来似乎是任意数据.这是生成的HTML表:

+----------+---------+---------+---------+---------+
|          |1        |3        |6        |7        |
+----------+---------+---------+---------+---------+
|1         |X        |         |         |         |
|3         |         |         |         |         |
|6         |         |         |         |         |
|7         |         |         |         |         |
+----------+---------+---------+---------+---------+
Run Code Online (Sandbox Code Playgroud)

此外,'X'位置与present值无关.

任何想法为什么会这样?

谢谢!

eri*_*sco 3

幸运的是,您只需要一个查询。假设 $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>
Run Code Online (Sandbox Code Playgroud)