PHP 中行的动态列数量

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

我有一个数据集,我想将其中 1 列中的行中的唯一值显示为列,以及另一行中的唯一集的第一列。然后用第三行的值和显示的列-行组合中的相应值填充矩阵。 但随后还在第二列中显示第二个数据集的值,该数据集也与显示的第一列相对应。

我在 Stackoverflow 上搜索了几个小时,但就是找不到正确的连接。 我确实知道我必须构建 2 个数组,但我就是不知道我做错了什么。甚至没有来总结和第二盘的额外链接。因为第一件事是行不通的。 我并不关心解决方案的类型。现在任何解决方案或指针都可以。

数据:

BLOEMEN table:
bloem_id bloem_naam
1120 Petra's Wedding
1145 White Aster
1225 My Love
1305 Caramel Antique
1315 Eveline
1330 Polar Ice
1340 Snow Cap  
1391 Snow Cap Deco
etc

knollen table:
bloem_id jaar teler_id plant_aant
1145 2023 Tjakan 400
1340 2023 Excellent 450
1340 2023 Fatal 5500
etc

Wanted output (arbitrary, not counts per cell not based on input:
The number of columns should be based on number of 'telers' that filled the amounts.
So can differ per year that they plant their flowers.
|      | Bloemsoort  | Klein cuba | Tjakan | Etc |
|------|-------------|------------|--------|-----|
| 1120 | White aster |            | 100    |     |
| 1145 | My love     | 140        | 250    |     |
|      | Total       | 140        | 350    |     |
<?php
include 'verbinding.php';
$jaar = date("Y");
$conn = new mysqli($host, $username, $password, $database); 
$SQL = "SELECT knollen.bloem_id, knollen.jaar, knollen.plant_aant, BLOEMEN.bloem_naam, knollen.teler_id       AS teler_id
                FROM knollen 
                INNER JOIN BLOEMEN
                ON knollen.bloem_id = BLOEMEN.bloem_id
                WHERE jaar = $jaar-1
                ORDER BY bloem_id";
     
$grouped = [];
$columns = [];    
       
$resultObject = $result = mysqli_query($conn, $SQL);
foreach ($resultObject as $row) {
    $grouped[$row['bloem_id']] = $row['plant_aant'];
    $columns[$row['teler_id']] = $row['teler_id'];
}
       
sort($columns);
$defaults = array_fill_keys($columns,'-');
array_unshift($columns, 'teler_id');
      
?>
 
    <table id="example" class="display" style="width:100%" align="center">
    <thead>
  
<?php
 
        while($result = mysqli_fetch_array($resultObject))
        {
            echo "<tr>
         <td>title 1</td> 
         <td>title 2</td>
         </tr>";
        }
?>
    </thead>
 
    <tbody>
 
<?php
 
        while($result = mysqli_fetch_array($resultObject))
        {
            echo "<tr>
         <td colspan='2'> ".$row['bloem_id']." </td>
         <td> ".$row['plant_aant']." </td> 
         
          </tr>";
        }
 
?>
php arrays row multiple-columns transpose
1个回答
0
投票

我能想到的最佳解决方案是循环数据库结果并重组它以便于在表中打印。 这还允许您计算底行的总数。

$grouped = [];
$bloemen = [];
$telers = [];

$resultObject = mysqli_query($conn, $SQL);
foreach ($resultObject as $row) {
    $grouped[$row['bloem_id']][$row['teler_id']] = $row['plant_aant'];
    $bloemen[$row['bloem_id']] = $row['bloem_naam'];
    if (isset($telers[$row['teler_id']])) {
        $telers[$row['teler_id']] += $row['plant_aant'];
    } else {
        $telers[$row['teler_id']] = $row['plant_aant'];
    }
}

ksort($telers);

$grouped
现在包含可用
flower + 'teler'
组合的植物数量。
$bloemen
包含键值对
bloem_id => bloem_naam
的数组,用于显示在前 2 列中。
$telers
包含键值对数组
teler_id => total
,用于显示在顶行和底行。

您只需要

ksort
(按数组键排序)
$telers
,由于查询中存在
$bloemen
,因此
ORDER BY
已经排序。

现在构建表格:

<table id="example" class="display" style="width:100%" align="center">
    <thead>
        <tr>
            <th></th>
            <th>Bloemsoort</th>
            <?php
                foreach ($telers as $teler_id => $total) {
                    echo "<th>{$teler_id}</th>";
                }
            ?>
        </tr>
    </thead>
    <tbody>
        <?php
            foreach ($bloemen as $bloem_id => $bloem_naam) {
                echo "<tr><td>{$bloem_id}</td><td>{$bloem_naam}</td>";
                foreach ($telers as $teler_id => $total) {
                    echo "<td>";
                    if (isset($grouped[$bloem_id][$teler_id])) {
                        echo $grouped[$bloem_id][$teler_id];
                    }
                    echo "</td>";
                }
                echo "</tr>";
            }
        ?>
        <tr>
            <td></td>
            <td>Total</td>
            <?php
                foreach ($telers as $teler_id => $total) {
                    echo "<td>{$total}</td>";
                }
            ?>
        </tr>
    </tbody>
</table>
© www.soinside.com 2019 - 2024. All rights reserved.