sql >> データベース >  >> RDS >> Mysql

動的行スパンでデータベースのデータを表示する方法

    まず第一に、私の英語が下手でごめんなさい。

    クエリでは、 id_locationで並べ替える代わりに 、コンポーネント名で並べ替える 。このようにして、動的な行スパンを簡単に追加できます。私はあなたの接続プログラムを変更していませんが、あなたの2番目の部分を変更しました。チェックしてください。私は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>";
    
        }
    ?>
    


    1. 1つの非常に大きなテーブルまたは3つの大きなテーブル? MySQLのパフォーマンス

    2. SQLクエリによる奇妙な速度の変化

    3. MySQLの階層クエリ。 (MySQLと同等のもので接続)

    4. 大量のデータでより速く実行できますか[MySQL]