0

我需要生成一个 PHP 页面,该页面以显示每个类似行项目的计数/摘要的方式显示存储在数组中(来自 MySQL 请求)的所有详细信息。

这是一个从 SQL 查询中收集的示例数据集(它的结构为 ORDER BY Workgroup, ProductCode):

+----+-------------+-------------+-----------+-----------+
| ID | ProductCode | Description | Workgroup |   Status  |
+----+-------------+-------------+-----------+-----------+
|  2 |     A123    |    Coke     |  Finance  |  Ordered  |
|  4 |     A123    |    Coke     |  Finance  |  Rejected |
|  1 |     A123    |    Coke     |  Finance  |  Rejected |
|  6 |     B111    |    Pepsi    |  Finance  |  Ordered  |
|  5 |     B111    |    Pepsi    |  Finance  |  Rejected |
|  8 |     B111    |    Pepsi    |  Finance  |  Rejected |
| 10 |     B112    |    Fanta    |  Finance  |  Ordered  |
| 42 |     A123    |    Coke     | Management|  Ordered  |
| 16 |     A123    |    Coke     | Management|  Ordered  |
| 11 |     B112    |    Fanta    | Management|  Rejected |
+----+-------------+-------------+-----------+-----------+

这些详细信息已存储在 PHP 数组中,如下所示:

    Array
    (
        [0] => Array
            (
                [ID] => 2
                [ProductCode] => A123
                [Description] => Coke
                [Workgroup] => Finance
                [Status] => Ordered
            )
        [1] => Array
            (
                [ID] => 4
                [ProductCode] => A123
                [Description] => Coke
                [Workgroup] => Finance
                [Status] => Rejected
            )
             ...etc
)

我需要的是在由 WorkGroup 分隔的多个表中吐出数据,每个产品有一个汇总行,下面列出每个单独的行项目(它不需要是一个 html 表,只是一些能够表示行项目)

从我展示的 10 个示例条目中,它需要输出以下内容:

+--------------------------------------------------------+
| FINANCE TEAM                                           |
+----+-------------+-------------+-----------+-----------+
| Coke      Total: 3     Ordered: 1     Rejected: 2      |
+----+-------------+-------------+-----------+-----------+
|  2 |     A123    |    Coke     |  Finance  |  Ordered  |
|  4 |     A123    |    Coke     |  Finance  |  Rejected |
|  1 |     A123    |    Coke     |  Finance  |  Rejected |
+----+-------------+-------------+-----------+-----------+
| Pepsi    Total: 3     Ordered: 1      Rejected: 2      |
+----+-------------+-------------+-----------+-----------+
|  6 |     B111    |    Pepsi    |  Finance  |  Ordered  |
|  5 |     B111    |    Pepsi    |  Finance  |  Rejected |
|  8 |     B111    |    Pepsi    |  Finance  |  Rejected |
+----+-------------+-------------+-----------+-----------+
| Fanta    Total: 1     Ordered: 1      Rejected: 0      |
+----+-------------+-------------+-----------+-----------+
| 10 |     B112    |    Fanta    |  Finance  |  Ordered  |
+----+-------------+-------------+-----------+-----------+

+--------------------------------------------------------+
| MANAGEMENT TEAM                                        |
+----+-------------+-------------+-----------+-----------+
| Coke      Total: 2     Ordered: 2     Rejected: 0      |
+----+-------------+-------------+-----------+-----------+
| 42 |     A123    |    Coke     | Management|  Ordered  |
| 16 |     A123    |    Coke     | Management|  Ordered  |
+----+-------------+-------------+-----------+-----------+
| Fanta      Total: 1    Ordered: 0     Rejected: 1      |
+----+-------------+-------------+-----------+-----------+
| 11 |     B112    |    Fanta    | Management|  Rejected |
+----+-------------+-------------+-----------+-----------+

如果我的要求不够清楚,我可以进一步详细说明。

任何帮助将不胜感激。

提前致谢。

4

1 回答 1

0

php 数组很容易转换成树状结构。您听起来好像您知道如何编程,所以我不会发送tehfullcodez,因为我认为您可以处理一些嵌套循环以在将其放入更方便的格式后自己生成最终的 html。

$tree = array();
foreach ($arr as $row) {
    extract($row);
    $tree[$Workgroup][$Description][] = $row;
}
print_r($tree);
于 2012-06-08T04:33:54.013 回答