Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Crazylife's avatar

unit converter for product

I have a question regarding to unit converter for product. I stored the following data as shown below

1 PCS  = 1 PCS
1 BOX = 10 PCS
1 CARTON = 40 PCS

database design

id | fromUnit | ratio | toUnit

I am converting every fromUnit to a base unit (PCS).

How should i design my database so that i know 1 BOX = ? CARTON? E.g 2 BOX = 0.5 CARTON. (Formula : 20/40 = 0.5)

Any linkage need to make based on this situation?

0 likes
8 replies
Tray2's avatar

I would probably calculate it on the fly instead. I don't see the need to have pcs since one pcs is always 1 pcs.

Something like this

id | product_id |box_qty | carton_qty
  SELECT 2 * box_qty / carton_qty as cartons
  FROM packings
  WHERE product_id = 1
Snapey's avatar

is it easier to think the other way around?

640 pcs per pallet
40 pcs per carton
10 pcs per box
 1 pcs per pcs

I assume if someone wants 20 of something, this should convert to 2 boxes?, 50 is 1 carton and 1 box? 12 is 1 box and 2 pcs

Do you need to convert between units or just know how many of each to most efficiently make up an order?

Crazylife's avatar

@SNAPEY - For example, pcs is always my base unit for conversion. But during display, will show 1carton = 10box = 100pcs

Snapey's avatar

Not sure where the problem is then?

You need two columns, 'container' and 'pcs'

Crazylife's avatar

@SNAPEY - Because i am storing only conversion rate to base. But i didnt store conversion between box and carton. I only know carton = how many pcs and box = how many pcs, but not 1 carton = how many box

Snapey's avatar

you can calculate box/carton ratio at any time

Crazylife's avatar

@SNAPEY - ya, right. Do i need attribute like parent id to know which is the biggest and smallest unit? E.g. Carton -> Box -> pcs

Snapey's avatar

I don't know your use case, but if you have more than product being converted you might need to store what this set of conversions relate to, otherwise no, why would you need to store the order when they are in the database with a column stating the number of pcs?

Please or to participate in this conversation.