我正在尝试制作拳击模块。 我有这样的数据。
RATIO
S:1 M:2 L:2 XL:1
案例1:
color SIZE S, M, L, XL RED 10 20 20 8 BLUE 5 10 10 5 WHITE 10 30 20 10
现在,如果我设置框数量= 30,那么我希望得到
box_no, Color S, M, L, XL 1 RED 5, 10, 10, 5 2 RED 5, 10, 10, 3 2 BLUE 0, 0, 0, 2 3 BLUE 5, 10, 10, 3 3 WHITE 0, 0, 0, 2 4 WHITE 5, 10, 10, 5 5 WHITE 5, 10, 10, 5 6 WHITE 0, 10, 0, 0
案例2:
color SIZE S, M, L, XL RED 1 2 2 2 BLUE 1 2 2 1 WHITE 1 3 2 1
现在,如果我设置框数量= 30,那么我希望所有内容都在1个框中
box_no, Color S, M, L, XL 1 RED 1 2 2 2 1 BLUE 1 2 2 1 1 WHITE 1 3 2 1
我如何用php实现这个目标?
我不是在问整个工作代码。我卡住了几个小时没有任何进展。 请帮助我如何开始或指导?
答案 0 :(得分:2)
您的比例目前是一个计数。把它变成适当的比例。
答案 1 :(得分:0)
我没有声明/初始化所有变量,也不关心格式化,但这是我的想法:
//Sum ratios
$ratioSum = $s + $m + $l +xl;
//Count how many slots a box should have (acorrding to the smalest piece)
$boxSlotCount = $qty/$ratioSum;
//Count the size of each box bins in slots according to ratio
$binS = $s * $boxSlotCount;
$binM = $m * $boxSlotCount;
$binL = $l * $boxSlotCount;
$binXL = $xl * $boxSlotCount;
//Loop while we have items in any of the big boxes.
$i = 1;
while ($S>0 || $M>0 || $L>0 || $XL>0)
{
echo $i;
//If the big boxes are hold more items than a little box bin can
//Than write the max size of a box bin
//Otherwise write the amount of items in the big box
echo ($S-$binS>0) ? $binS : $S;
echo ($M-$binM>0) ? $binM : $M;
echo ($L-$binL>0) ? $binL : $L;
echo ($XL-$binXL>0) ? $binXL : $XL;
//Subtract the items we put from the big boxes to the little ones
$S -= $binS;
$M -= $binM;
$L -= $binL;
$XL -= $binXL;
$i++;
}