如何以这种格式在 HTML 表格中打印这些数据?

问题描述 投票:0回答:2

假设你有这样的 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

并且您想要打印一个考虑到 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”是我的表中不存在的字段。

如果我这样说:

$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 次。

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

php sql mysql loops pivot
2个回答
0
投票

下面的代码可以解决这个问题,但这取决于数据库和查询的内容。确保每个

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;
}

0
投票

不是最优雅的,但您可以首先对每个集合进行分组,然后循环遍历分组数据以进行输出。然而,使用 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>";
            
            // Iterate over each value
            foreach($groupData as $value) {
                $output .= "<td>" . $value . "</td>";
            }
        $output .= "</tr>";
    }
$output .= "</table>";


// Output the HTML
'<pre>' . print_r($output, true) . '</pre>';
© www.soinside.com 2019 - 2024. All rights reserved.