I have two tables in MySql database, Named Items and Types, In Items I have Chair, table, etc,. In Types I have Wood and Iron.
+--------+-----------+
| ItemID | Item_Name |
+--------+-----------+
| 1 | Chair |
| 2 | Table |
| 3 | Bed |
+--------+-----------+
+--------+-----------+
| TypeID | Item_Type |
+--------+-----------+
| 1 | Iron |
| 2 | Wood |
+--------+-----------+
Now I want to print this in a php table as follows,
+------------+---------------+---------------+
| Item Name | Item Type One | Item Type Two |
+------------+---------------+---------------+
| Chair | Iron | Wood |
| Table | Iron | Wood |
| Bed | Iron | Wood |
+------------+---------------+---------------+
I tried this code:
<?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>
But its giving output like this:
+-----------+---------------+---------------+
| Item Name | Item Type One | Item Type Two |
+-----------+---------------+---------------+
| Chair | Iron | Wood |
| Table | | |
| Bed | | |
+-----------+---------------+---------------+
Only Printing the first row as it should be. What is the problem? How can I achive this?
The problem is, the result set $query_Type
is exhausted in the first row itself. You have to adjust the result pointer to point to the beginning of the result set so that you could iterate through it again. Make use of mysqli_result::data_seek
to adjust the result pointer.
Do $query_Type->data_seek(0);
right after the inner while
loop.
// 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>
I think that your data model or expectation needs to be adjusted, because I don't see any clean way or joining the Items
and Types
tables to generate the output table you expect. That being said, you could do a cross join of the two tables, followed by a pivot on the TypeID
column to generate what you want.
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
Output:
+------------+---------------+---------------+
| Item Name | Item_Type_One | Item_Type_Two |
+------------+---------------+---------------+
| Chair | Iron | Wood |
| Table | Iron | Wood |
| Bed | Iron | Wood |
+------------+---------------+---------------+
Demo here:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.