3

我是 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   

}
?>

这是我得到的输出: 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

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

4

2 回答 2

2

首先对我糟糕的英语感到抱歉。

在您的查询中,不要按id_location进行排序,而是按组件名称进行 排序。这样您就可以轻松添加动态行跨度。我没有改变你的连接程序,但我改变了你的第二部分。请检查。我知道有 3 到 4 个循环。但如果有任何机构发现更好的算法,请告诉我。

    $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'];

?>



<?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.component_name";
    $stmt = $dbc->prepare($query);
    $stmt->execute();

    # Declare two emty array
    $component     = array(); # Will store the components
    $sub_component = array(); # Will store the sub components

    $loop = 0;

    # Now if any data is fetched from previsous query, then fill
    # the above declared arrays.
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        $component[$loop]     = $row['component'];
        $sub_component[$loop] = $row['sub_component'];

        $loop = $loop + 1;
    }

    # If no data fetched then I m telling 
    # No data fetched.
    if (!sizeof($component)) {
        echo 'Empty Data';
    } else {

        print "<table width='469px' border='1'>
                    <tr bgcolor='#00FFFF'>
                        <th width='109' class='rounded' scope='col'>Component</th>
                        <th width='109' class='rounded' scope='col'>Sub Component</th>
                    </tr>";

        # Now our main logic starts to print dynamic rowspan

        # Go for a loop.
        # Here the imporant is to use for loop

        $tmp_arr = array();

        $main_assoc_arr = array();

        for ($i = 0; $i < sizeof($sub_component); $i++) {

            array_push($tmp_arr, $sub_component[$i]);

            # If we have reached the last element
            # and in $main_assoc_arr the comonent is not exist
            # Then we will store them as following.
            if (   $i = (sizeof($sub_component)-1)
                && !array_key_exists($component[$i], $main_assoc_arr)) {

                $main_assoc_arr[ $component[$i] ] = array();
                $main_assoc_arr[ $component[$i] ] = $tmp_arr;

                # Restore the array.
                $tmp_arr = array();

                # Also get out of the loop
                break;
            }

            # If the present component is not equal to the 
            # Next component then 
            if ($component[$i] != $component[$i+1]) {

                $main_assoc_arr[ $component[$i] ] = array();
                $main_assoc_arr[ $component[$i] ] = $tmp_arr;

                # Restore the array.
                $tmp_arr = array();
            }
        }

        # Now we are going to print the table with rowspan.
        foreach ($main_assoc_arr as $comp=>$sub_comp) {

            $printed = 0;
            $rowspan = sizeof($sub_comp);

            foreach ($sub_comp as $elm) {

                print "<tr>";

                # Manke sure that the column will not print
                # in each loop as it conatins dynamic array.
                if (!$printed) {

                    print "<td rowspan='$rowspan'>$comp</td>";
                }

                print "<td>$elm</td>"
                print "</tr>";
            }
        }

        print "</table>";

    }
?>
于 2015-01-17T16:29:30.167 回答
0

你的变量$result[][]在循环结束时不会被擦除或清理。

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

于 2014-12-02T22:24:36.070 回答