如何显示与 1 table 不同的 table?就像每个 table 都有唯一的代码

How do I display different table from 1 table? Like each table has unique code

这是我的数据库 table:

但我的查询只为每个 table 得到 1 行,如下所示:

如您所见,1003 有 2 个 table,因为它有 2 行。它应该只有 1003 个中的一 (1) table 个,有 2 行。我该如何解决?预期结果:

            // Attempt select query execution
            $query = "SELECT model, brand_code FROM smartphone GROUP BY model";
            if($result = mysqli_query($db, $query))
            {
                if(mysqli_num_rows($result) > 0)
                {
                    while($row = mysqli_fetch_array($result))
                    {
                    ?>
                    <?php echo $row["brand_code"]?>
                    <table id="table_stock" class="">
                      <thead>
                          <tr>
                              <th>Model</th>
                          </tr>
                      </thead>
                      <tbody>
                          <tr>
                              <td><?php echo $row["model"]?></td>
                          </tr>
                      </tbody>
                    </table><br>
                    <?php 
                    }
                    /// Free result
                    mysqli_free_result($result);
                } 
                else
                {
                    echo "<td class='no_record' colspan='7'>No records found.</td>";
                }
            } 
            else
            {
                echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
             }
            

您需要额外的循环。同样在第一个查询中,您需要按代码使用分组。

        $query = "SELECT model, brand_code FROM smartphone GROUP BY brand_code";
        if($result = mysqli_query($db, $query))
        {
            if(mysqli_num_rows($result) > 0)
            {
                while($row = mysqli_fetch_array($result))
                {
 ?>
                <?php echo $row["brand_code"]?>
                <table id="table_stock" class="">
                  <thead>
                      <tr>
                          <th>Model</th>
                      </tr>
                  </thead>
                  <tbody>

 <?php
                    if ($result1 = mysqli_query($db, "SELECT DISTINCT model, brand_code FROM smartphone WHERE brand_code={$row["brand_code"]}")) 
                    {
                       while ($row1 = mysqli_fetch_array($result1))
                       {                               
                             // get count for each model within brand_code
                             $cnt = ($result2 = mysqli_query($db, "SELECT COUNT(*) AS cnt FROM smartphone WHERE brand_code={$row["brand_code"]} AND model='{$row1["model"]}'")) && ($row2 = mysqli_fetch_array($result2)) ? $row2["cnt"] : "---";
                ?>
                      <tr>
                          <td><?php echo $row1["model"] ({$cnt})?></td>
                      </tr>
                <?php 
                      }
                     mysqli_free_result($result1);
                  }
 ?>
                  </tbody>
                </table><br>
 <?php
               }

                /// Free result
                mysqli_free_result($result);
            } 
            else
            {
                echo "<td class='no_record' colspan='7'>No records found.</td>";
            }
        } 
        else
        {
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
         }

你这里至少有5个问题,

[编辑:问题 1 根据扩展答案删除并更改了样本]


在您的 while { ... } 循环中,您正在打印整个 table,而您应该只打印那里的 <tr>...</tr> 部分。这就是导致额外 table(s).

的原因

第三个问题:您的“no_record”行松动 <td>。它不仅不在 table 内(在问题 #2 中有介绍),它也没有用 <tr>.

包裹

第 4 个问题:您在 table 之外随机打印 echo $row["brand_code"]


第 5 个问题:您正在从数据库中打印原始数据,好像它是有效的 html,但很可能不是。它可能必须用 htmlentities/htmlspecialchars.

编码

Quick & dirty 固定版本:

function tableOpen($row) {
    printf(  '<h1>%s</h1>', htmlentities($row["brand_code"]) );
    echo '<table id="table_stock" class="">';
    echo '<thead>';
    echo '<tr>';
    echo '<th>Model</th>';
    echo '</tr>';
    echo '</thead>';
    echo '<tbody>';
}
function tableClose() {
    echo '</tbody>';
    echo '</table><br>';
}
// Attempt select query execution
$query = "SELECT model, brand_code FROM smartphone ORDER BY brand_code";
$lastBrand = null;
if ($result = mysqli_query($db, $query)) {
    if (mysqli_num_rows($result) > 0) {
        if ($lastBrand !== $row["brand_code"] && !is_null($lastBrand)) tableClose();
        if ($lastBrand !== $row["brand_code"]) tableOpen($row);
        $lastBrand = $row["brand_code"];
        while ($row = mysqli_fetch_array($result)) {
            echo   '<tr>';
            printf(  '<td>%s</td>', htmlentities($row["model"]) );
            echo   '</tr>';
        }
        tableClose();
        /// Free result
        mysqli_free_result($result);
    } else {
        echo '<p class="no_record">No records found.</p>';
    }
} else {
    echo "ERROR: Not able to execute $query: <br>" . htmlentities($query) . '<br>' . htmlentities(mysqli_error($link));
}