php 我怎样用这种格式在HTML表格中打印这些数据?

xoefb8l8  于 2023-01-24  发布在  PHP
关注(0)|答案(2)|浏览(146)

假设您有以下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次。
是否可以这样做,或者我是否应该重新构造数据库,以另一种方式存储信息?

gt0wga4j

gt0wga4j1#

下面的代码可以完成这个任务,但是它取决于数据库和查询的内容。确保每个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;
}
wztqucjr

wztqucjr2#

虽然不是最好的方法,但是你可以先对每个集合进行分组,然后循环输出分组后的数据,但是使用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>';

相关问题