2017-08-27 50 views
0

我在MySql數據庫中有兩個表格,命名項目和類型,項目中有椅子,表格等。在類型我有木材和鐵。如何在單表格中打印多個表格中的值php

項目表

+--------+-----------+ 
| 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  |    |    | 
+-----------+---------------+---------------+ 

只打印時,因爲它應該是第一行。 問題是什麼?我怎樣才能做到這一點?

回答

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

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>