如何在php

时间:2017-08-27 13:41:39

标签: php mysql

我在MySql数据库中有两个表,命名项和类型,在项目中我有椅子,桌子等。在类型中我有木头和铁。

项目表

+--------+-----------+
| ItemID | Item_Name |
+--------+-----------+
|    1   | Chair     |
|    2   | Table     |
|    3   | Bed       |
+--------+-----------+

类型表

+--------+-----------+
| TypeID | Item_Type |
+--------+-----------+
|    1   | Iron      |
|    2   | Wood      |
+--------+-----------+

现在我想在php表中打印,如下所示,

新表

+------------+---------------+---------------+
| Item Name  | Item Type One | Item Type Two |
+------------+---------------+---------------+
| Chair      | Iron          | Wood          |
| Table      | Iron          | Wood          |
| Bed        | Iron          | Wood          |
+------------+---------------+---------------+

我试过这段代码:

<?php
  $query_Item = $db->query("SELECT * FROM tb_Items");
  $itemsRowCount = $query_Item->num_rows;

  $query_Type = $db->query("SELECT * FROM tb_Types");
  $typesRowCount = $query_Type->num_rows;
?>
<table border="1">
  <tr>
    <th>Item Name</th>
    <th>Item Type One</th>
    <th>Item Type Two</th>
  </tr>
  <?php
  if($itemsRowCount > 0){
    while($row = $query_Item->fetch_assoc()){
  ?>
  <tr>
    <td><?php echo $row['ItemName']; ?></td>
    <?php
    if($typesRowCount > 0){
      while($row = $query_Type->fetch_assoc()){
    ?>
    <td><?php echo $row['ItemType']; ?></td>
    <?php
      }
    }
    ?>
  </tr>
    <?php
    }
  }
?>
</table>

但它的输出如下:

+-----------+---------------+---------------+
| Item Name | Item Type One | Item Type Two |
+-----------+---------------+---------------+
| Chair     | Iron          | Wood          |
| Table     |               |               |
| Bed       |               |               |
+-----------+---------------+---------------+

仅打印第一行。 问题是什么?我怎么能做到这一点?

2 个答案:

答案 0 :(得分:1)

问题是,结果集$query_Type在第一行本身已用完。您必须将结果指针调整为指向结果集的开头,以便您可以再次遍历它。利用mysqli_result::data_seek调整结果指针。

在内部$query_Type->data_seek(0);循环之后立即while

// your code
<table border="1">
    <tr>
        <th>Item Name</th>
        <th>Item Type One</th>
        <th>Item Type Two</th>
    </tr>
    <?php
        if($itemsRowCount > 0){
            while($row = $query_Item->fetch_assoc()){
        ?>
        <tr>
            <td><?php echo $row['ItemName']; ?></td>
            <?php
                if($typesRowCount > 0){
                    while($row = $query_Type->fetch_assoc()){
                ?>
                    <td><?php echo $row['ItemType']; ?></td>
                <?php
                    }
                    $query_Type->data_seek(0);
                }
            ?>
        </tr>
        <?php
            }
        }
    ?>
</table>

答案 1 :(得分:1)

我认为您的数据模型或期望需要调整,因为我没有看到任何干净的方式或加入ItemsTypes表来生成您期望的输出表。话虽这么说,你可以对两个表进行交叉连接,然后在TypeID列上设置一个数据透视表来生成你想要的内容。

SELECT
    t1.Item_Name,
    MAX(CASE WHEN t2.TypeID = 1 THEN t2.Item_Type END) AS Item_Type_One,
    MAX(CASE WHEN t2.TypeID = 1 THEN t2.Item_Type END) AS Item_Type_Two
FROM Items t1
INNER JOIN Types t2
GROUP BY
    t1.ItemID,
    t1.Item_Name

<强>输出:

+------------+---------------+---------------+
| Item Name  | Item_Type_One | Item_Type_Two |
+------------+---------------+---------------+
| Chair      | Iron          | Wood          |
| Table      | Iron          | Wood          |
| Bed        | Iron          | Wood          |
+------------+---------------+---------------+

在这里演示:

SQLFiddle