Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
SQL
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...
Posted
Updated 5-Mar-22 22:28pm
v18

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 while loop. Afterwards,
$uom_price = explode(",", $product['uom_price']);
$price_price = explode(",", $product['price']);
echo $uom_price[0].':'.$price_price[0].' ';
if(!empty($uom_price[1])){
   echo $uom_price[1].':'.$price_price[1].' ';
    }
if(!empty($uom_price[2])){
   echo $uom_price[2].':'.$price_price[2].' ';
  }
 
Share this answer
 
You need to use the JOIN clause to select items from multiple tables at the same time. See the description and examples at MySQL Joins[^].
 
Share this answer
 
Comments
FRS4002 5-Mar-22 4:09am    
@Richard MacCutchan Could you please provide me for the right MYSQL code for my situation?
Richard MacCutchan 5-Mar-22 4:52am    
Something like:
SELECT products.itemNo, products.title, uom.uom, products_uom.price, products.quantity
FROM ((products
INNER JOIN uom ON products_uom.uom_id = uom.id)
INNER JOIN products_uom ON products_uom.product_id = products.id);

However, I do not have any way to test this so you may need to modify it. Also, I would suggest you spend time studying SQL syntax in general, and MySQL in particular, if you intend to use this in your career.
FRS4002 5-Mar-22 7:22am    
Thanks, but weird, it says "Unknown column 'products_uom.uom_id' in 'on clause'". Also there are 4 kinds of joins, which to use in my case? Is it only inner join?
Richard MacCutchan 5-Mar-22 6:58am    
Sorry, I had the JOINS out of order, try:
SELECT products.itemNo, products.title, uom.uom, products_uom.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);
FRS4002 5-Mar-22 7:27am    
Thanks, now I tried the code in phpmyadmin and I didn't get any issues. Now pardon me, you didn't answer my question above, there are 4 kinds of joins, which to use in my case? Is it only inner join? Also, now if I want to implement this in PHP I am thinking to do a while loop correct me if I am wrong
while($product = mysqli_fetch_assoc($presults)){-----}

Now how can I display a specific value from joins? like this?
$product['uom.uom'];

Does this works or there is a right way?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900