I want to link and display values from 3 normalized tables using PHP and MYSQL in an HTML table. 1st table is called uom:
id | uom
1 | piece
2 | dozen
3 | set
2nd table called products:
id | itemNo | title | quantity
84 | 19421 bags 10
88 | 7878 chests 20
89 | 123456 test 30
3rd table is called products_uom:
id | product_id | uom_id | quantity_package | price
1 | 84 | 1 | 1 | 10.00
2 | 84 | 2 | 12 | 20.00
3 | 84 | 3 | 1 | 30.00
4 | 88 | 1 | 1 | 60.00
and here is my HTML table:
<table border='1'>
<tr>
<th>item No.</th>
<th>title</th>
<th>uom and price</th>
<th>quantity</th>
</tr>
Now in uom and price I need to display all uom of a product with its price + display the uom as text not id (uom column) like this (piece:10 dozen:20) for bags.
What I have tried:
I made this code:
SELECT products.id, products.itemNo, products.title,
GROUP_CONCAT(uom.uom)
AS uom_price,
GROUP_CONCAT(products_uom.price)
AS price, products.quantity
FROM ((products
INNER JOIN products_uom
ON products_uom.product_id = products.id)
INNER JOIN uom
ON products_uom.uom_id = uom.id)
GROUP BY products.id
and then I did a while loop afterwards, and then when I reached to uom and price, I did that,
$uom_price = explode(",", $product['uom_price']);
$price_price = explode(",", $product['price']);
echo $uom_price[0].':'.$price_price[0].' ';
echo $uom_price[1].':'.$price_price[1].' ';
echo $uom_price[2].':'.$price_price[2].' ';
and that's the result https://jsfiddle.net/vapfchr6/ and this what I want to achieve https://jsfiddle.net/aczshequ/ it is almost exactly what I wanted but there is an issue. In uom and price for chests, it displays piece:60.00 :: with 2 colons, how to fix this issue? I think I need to do if statement if there are more uoms display the colon, but I don't know how to do it in my code above... Also there is an optional issue, I need to display the prices without the double zeros at the end 10.00 > 10 unless there are numbers...