如何使用此格式在HTML表中打印此数据?

发布于 2025-02-04 12:01:03 字数 1960 浏览 6 评论 0原文

想象一下,您有此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>";

这也不起作用,因为“值”的值将重复3次。

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

Imagine that you have this SQL query result:

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

And you want to print a table taking into account the ID_KEY, like this:

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

How can I do that? I want to print a new row when ID_KEY changes.
For example, now I have this code:

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

This code won't work because "value1", "value2" and "value3", are fields that do not exist in my table.

If instead I say this:

$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>";

This won't work either, as the value of "VALUE" will be repeated 3 times.

Is it possible to do this or should I restructure the database to store the information in another way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许仙没带伞 2025-02-11 12:01:03

下面的代码可以解决问题,但取决于数据库和查询的内容。确保总有3个值每个ID_KEY,并且结果进行正确排序。这个想法是,您重复具有值的输出单元格,直到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;
}

The code below would do the trick, but it is dependent on the content of your database and the query. Make sure there are always 3 values per ID_KEY and that the results are sorted properly. The idea is that you repeat to output cells with a value, until the ID_KEY changes. Many minor variations are possible, but in the end they all work on this principle.

$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;
}
老娘不死你永远是小三 2025-02-11 12:01:03

不是最优雅的,但是您可以先进行每组,然后通过分组数据进行循环以进行输出。但是,使用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>';

Not the most elegant but you could group each set first and and loop through the grouped data for the output. However it would probably more efficient using SQL to group the data this way.

<?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>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文