如何使用动态rowspan显示数据库中的数据

时间:2013-05-12 14:08:41

标签: php html mysql

在php编程中我是新手,我有一些使用php和html显示来自mysql数据库的数据的问题。这是我的表:

location :id_location
          location

component :id_comopnent
           id_location
           comonen

sub_component :id_sub_component
               id_comopnent
               sub_component

我如何获得以下输出:

    location 
    |-----------|-------------|
    | component |sub component|
    |-----------|-------------|
    |           |    Data     |
    |   Data    |-------------|
    |           |    Data     |
    |-----------|-------------|
    |           |    Data     |
    |           |-------------|
    |   Data    |    Data     |
    |           |-------------|
    |           |    Data     |
    |-----------|-------------|

    location 

    |-----------|-------------|
    | component |sub component|
    |-----------|-------------|
    |           |    Data     |
    |   Data    |-------------|
    |           |    Data     |
    |-----------|-------------|
    |           |    Data     |
    |   Data    |-------------|
    |           |    Data     |
    |-----------|-------------|

    Location (according to the data)

这是我的代码

 <?php
   $dsn = "mysql:host=localhost;dbname=kampus_hijau";
   $dbc = new PDO($dsn, 'root', '');

    $sql1 = "SELECT * FROM Lokasi ORDER BY id_location";
    $stmt1 = $dbc->prepare($sql1);
    $stmt1->execute();

    while ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
    $location++;
    echo "Location $location : ".$row1['location'];

  ?>
<table width="469" border="1">
  <tr bgcolor="#00FFFF">
    <th width="109" class="rounded" scope="col">Component</th>
    <th width="248" class="rounded" scope="col">Sub Component</th>
    <th>Nilai</th>

     </tr>
  <?php

    $query = "SELECT * FROM sub_component,component where sub_component.id_component=component.id_component and  component.id_location='$data[id_location]' order by component.id_location";
    $stmt = $dbc->prepare($query);
    $stmt->execute();

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $result[$row['component']][] = $row['sub_component'];

    }

                foreach($result as $id => $invoices) {
                        echo '<td rowspan='. count($invoices) . '>' . $id . '</td>';
                        $count = 0;
                        foreach ($invoices as $invoice) {
                            if ($count != 0) {
                                echo '<tr>';
                            }
                            echo "<td>$invoice</td>";
                            echo "</tr>";
                            $count++;
                }
                    }
?>

</table>
<?php   

}
?>

这是我得到的输出: enter image description here enter image description here enter image description here

使用该代码,输出形式是合适的,但数据看起来不合适,它总是显示下一个表中的数据(红色框是重复的数据) )。我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的变量$result[][]在循环结束时无法删除或清除。

在第一个while循环(while ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {)中,执行另一个循环来填充$ result数组(while ($row = $stmt->fetch(PDO::FETCH_ASSOC)))。当第一个while循环开始其第二轮时,你的第二个while循环将把新行堆叠在旧循环之上。因此,请确保第一个循环结束时$result[][]数组为空。