我怎样才能用这种格式在 HTML table 中打印这些数据?

How can I print this data in a HTML table with this format?

假设您有这个 SQL 查询结果:

ID     ID_KEY     VALUE

1      1          Text1.1
2      1          Text1.2
3      1          Text1.3
4      2          Text2.1
5      2          Text2.2
6      2          Text2.3
7      3          Text3.1
8      3          Text3.2
9      3          Text3.3

你想打印一个 table 考虑到 ID_KEY,像这样:

ID_KEY     VALUE1     VALUE2     VALUE3

1          Text1.1    Text1.2    Text1.3
2          Text2.1    Text2.2    Text2.3
3          Text3.1    Text3.2    Text2.3

我该怎么做?我想在 ID_KEY 更改时打印一个新行。 例如,现在我有这个代码:

    $result = $con->query($sql);
    
    if ($result->num_rows > 0) {
        $res = "<table>";
            $res .= "<tr>";
                $res .= "<th>ID_KEY</th>";
                $res .= "<th>VALUE1</th>";
                $res .= "<th>VALUE2</th>";
                $res .= "<th>VALUE3</th>";
            $res .= "</tr>";
            while ($row=mysqli_fetch_assoc($result)) {
                $res .= "<tr>";
                    $res .= "<td>" . $row['ID_KEY'] . "</td>";
                    $res .= "<td>" . $row['VALUE1'] . "</td>";
                    $res .= "<td>" . $row['VALUE2'] . "</td>";
                    $res .= "<td>" . $row['VALUE3'] . "</td>";
                $res .= "</tr>";
            }
        $res .= "</table>";

        return $res;
    }

此代码无效,因为“value1”、“value2”和“value3”是我的 table 中不存在的字段。

如果我这样说:

$res .= "<tr>";
    $res .= "<td>" . $row['ID_KEY'] . "</td>";
    $res .= "<td>" . $row['VALUE'] . "</td>";
    $res .= "<td>" . $row['VALUE'] . "</td>";
    $res .= "<td>" . $row['VALUE'] . "</td>";
$res .= "</tr>";

这也不行,因为“VALUE”的值将重复 3 次。

是否可以这样做,或者我是否应该重组数据库以另一种方式存储信息?

下面的代码可以解决问题,但这取决于您的数据库和查询的内容。确保每个 ID_KEY 始终有 3 个值,并且结果排序正确。这个想法是你重复输出一个值的单元格,直到 ID_KEY 改变。许多微小的变化都是可能的,但最终它们都遵循这个原则。

$result = $con->query($sql);

if ($result->num_rows > 0) {
    $res = "<table>";
        $res .= "<tr>";
            $res .= "<th>ID_KEY</th>";
            $res .= "<th>VALUE1</th>";
            $res .= "<th>VALUE2</th>";
            $res .= "<th>VALUE3</th>";
        $res .= "</tr>";
        $row = mysqli_fetch_assoc($result);
        while ($row) {
            $idKey = $row['ID_KEY'];
            $res .= "<tr>";
            $res .= "<td>" . $idKey . "</td>";
            $res .= "<td>" . $row['VALUE'] . "</td>";
            while (($row = mysqli_fetch_assoc($result)) &&
                   ($idKey == $row['ID_KEY'])) {
                $res .= "<td>" . $row['VALUE'] . "</td>";
            }
            $res .= "</tr>";
        }
    $res .= "</table>";

    return $res;
}

不是最优雅的,但您可以先对每个集合进行分组,然后循环遍历分组数据以获取输出。但是,使用 SQL 以这种方式对数据进行分组可能会更有效。

<?php

// Original Data
$originalData = [
    [
        'ID' => 1,
        'ID_KEY' => 1,
        'VALUE' => "Text1.1"
    ],
    [
        'ID' => 2,
        'ID_KEY' => 1,
        'VALUE' => "Text1.2"
    ],
    [
        'ID' => 3,
        'ID_KEY' => 1,
        'VALUE' => "Text1.3"
    ],
    [
        'ID' => 4,
        'ID_KEY' => 2,
        'VALUE' => "Text2.1"
    ],
    [
        'ID' => 5,
        'ID_KEY' => 2,
        'VALUE' => "Text2.2"
    ],
    [
        'ID' => 6,
        'ID_KEY' => 2,
        'VALUE' => "Text2.3"
    ],
    [
        'ID' => 7,
        'ID_KEY' => 3,
        'VALUE' => "Text3.1"
    ],
    [
        'ID' => 8,
        'ID_KEY' => 3,
        'VALUE' => "Text3.2"
    ],
        [
        'ID' => 9,
        'ID_KEY' => 3,
        'VALUE' => "Text3.3"
    ],
];

// Group the Data by ID_KEY
$groupedData = [];

foreach($originalData as $item) {
    $groupedData[$item['ID_KEY']][] = $item['VALUE'];
}

// HTML Output  
$output = "<table>";
    $output .= "<tr>";
        $output .= "<th>ID_KEY</th>";
        $output .= "<th>VALUE1</th>";
        $output .= "<th>VALUE2</th>";
        $output .= "<th>VALUE3</th>";
    $output .= "</tr>";

    // Output each row
    foreach($groupedData as $idKey => $groupData) {
         $output .= "<tr>";
            $output .= "<td>" . $idKey . "</td>";
            
            // Iterrate of each value
            foreach($groupData as $value) {
                $output .= "<td>" . $value . "</td>";
            }
        $output .= "</tr>";
    }
$output .= "</table>";


// Output the HTML
'<pre>' . print_r($output, true) . '</pre>';