簡體   English   中英

如何將不同的數據合並到一個具有多列的表行中?

[英]How to combine different data into one table row with multiple columns?

我有這個RAW表:

====================================================
| id | name      | fieldid | info1     | info2     |
====================================================
| 1  | testname1 | 1       | testing 1 | testing 2 |
|----|-----------|---------|-----------|-----------|
| 2  | testname2 | 2       | testing 3 | testing 4 |
|----|-----------|---------|-----------|-----------|
| 3  | testname2 | 2       | testing 5 | testing 6 |
====================================================

並且我想使用“名稱”和“字段ID”作為表格的參考,使其看起來像這樣:

=======================================
| id | name      | fieldid | info     |
=======================================
| 1  | testname1 | 1       | testing1 |
|    |           |         | testing2 |
|----|-----------|---------|----------|
| 2  | testname2 | 2       | testing3 |
|    |           |         | testing4 |
|    |           |         | testing5 |
|    |           |         | testing6 |
=======================================



我的代碼當前生成此:

=========================================
| id | name      | fieldid   | info     |
=========================================
| 1  | testname1 | 1         | testing1 |
|    |           |           | testing2 |
|----|-----------|-----------|----------|
| 2  | testname2 | 2         | testing3 |
|    |           |           | testing4 |
|    |           |           | testing6 |
|----|           |-----------|----------|------------
| 3  |           | testname2 | 2        | testing 5 |
|    |           |           |          | testing 4 |
|    |           |           |          | testing 6 |
=====================================================

這是代碼:

<?php $counter = null;?>
    <?php
        $code1 = "SELECT * FROM tableq";
        $query1 = $conn->query($code1) or trigger_error($conn->error.[$code1]);

        echo ("
            <table>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Field ID</th>
                    <th>Info</th>
                </tr>
        ");

        if($query1->num_rows > 0){
            while($row1 = $query1->fetch_assoc()){
                echo ("
                    <tr>
                        <td>".$row1['id']."</td>
                ");

                if($counter == ""){
                    $counter = "1";
                }
                else if($counter == "4"){
                    $counter = $counter / 2;
                }

                $code2 = "SELECT * FROM tableq WHERE name='" . $row1['name'] . "' AND fieldid='" . $row1['fieldid'] . "'";
                $query2 = $conn->query($code2) or trigger_error($conn->error.[$code2]);

                if($query2->num_rows > 0){
                    echo ("
                            <td rowspan=\"".$counter."\">".$row1['name']."</td>
                    ");
                }

                echo ("
                        <td>".$row1['fieldid']."</td>
                        <td>".$row1['info']."
                ");

                if($query2->num_rows > 0){
                    while($row2 = $query2->fetch_assoc()){
                        $counter += 1;
                        echo ("
                                <br>".$row2['info2']."
                        ");
                    }
                }else{
                    die("No results found");
                }
                echo ("
                        </td>
                    </tr>
                ");
            }
        }
    ?>

我已經閱讀了相關的問題,但是我的問題並沒有解決。

我建議以其他方式解決它。

您要做的第一件事是建立一個多維數組,例如

$entries[1][testName1][fieldId] = [entry1, entry2, entry3];
$entries[2][testName2][fieldId] = [entry1, entry2, entry3];
$entries[3][testName2][fieldId] = []; // "Not found"

一旦構建了數組,就可以更輕松地在HTML表中輸出。 調試起來容易得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM