Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据尺寸制作包装_Php - Fatal编程技术网

Php 根据尺寸制作包装

Php 根据尺寸制作包装,php,Php,我正试图根据数据库中的信息创建数据包 数据库的组织方式如下: ID ORDERID DIMENSION QUANTITY 1 21 8400 25 2 21 7200 8 3 21 4300 5 4 21 3800 7 // assuming you have collected the results from your sql query like this: while($r = $q-&g

我正试图根据数据库中的信息创建数据包

数据库的组织方式如下:

ID ORDERID DIMENSION QUANTITY 
1    21    8400       25
2    21    7200       8
3    21    4300       5
4    21    3800       7
// assuming you have collected the results from your sql query like this:
while($r = $q->tech()) $dat[]=array($r['DIMENSION'],$r['QUANTITY']);

// you will end up with an array $dat: (dimension, quantity) like this:
// $dat=array(array(8400,25),array(7200,8),array(4300,5),array(3800,7));
// package size:
$n=10;

$cnt=0;                        // initialize package counter
$pack=array();                 // set up first package
$d=array_shift($dat);          // get first lot $d from $dat

do {     
  if ($d[1]>0){    
    $take=min($d[1],$n-$cnt);  // decide how much to take out ...
    $cnt+=$take;               // increase package counter
    $d[1]-=$take;$pack[]=array($d[0],$take); // tranfer the goods ...
                               // if package is full, start next one
    if ($cnt==$n) {$packs[]=$pack; $pack=array(); $cnt=0;}
  } else if(count($dat)>0){    // still elements in $dat left?
    $d=array_shift($dat);      // get the next lot $d from $dat
  } else break;    
} while(true);
if ($cnt>0) $packs[]=$pack;    // collect the last "stranded" package ...

echo str_replace("]],","]],\n ",json_encode($packs));    // as JSON string
// print_r($packs);            // conventional (bulky) PHP object notation
首先,从数据库获取信息

$sql = "SELECT ID, ORDERID, DIMENSION, QUANTITY FROM dimension WHERE ORDERID = 21 ORDER BY DIMENSION DESC";

$q = $conn => query($sql);

while($r = $q->tech()){
  $dim  =$r['DIMENSION '];
  $quant  =$r['QUANTITY '];
}
然后我从另一个表中检索数据,例如,我得到数字10

$query = "SELECT QUANTITY FROM panel WHERE ID = 21; 

$q = $conn->query($query);

while ($r=$q->fetch()){
  $number = 10;
}
从最长尺寸进行包装是非常重要的

例如,我有尺寸8400数量25。面板的最大数量是10,这意味着每10个面板将有两个数据包,剩余5个。 5转到下一个维度,我们将该面板的最大数量设置为$number。该维度的剩余部分进入下一维度,并保持不变

一旦我得到了包装所需的所有信息,最后包装解决方案应该是

-------------------
8400 x 10 - Packet1
-------------------
8400 x 10 - Packet2
-------------------
8400 x 5
7200 x 5 - Packet3
-------------------
7200 x 3
4300 x 5
3800 x 2 - Packet4
-------------------
3800 x 5 - Packet 5
-------------------
一旦我得到这些数据,我应该将其插入数据库 其中8400为尺寸,10,面板,“包装1”-名称


我被卡住了,不知道该怎么做。我不知道这个算法是否存在

您可以这样做:

ID ORDERID DIMENSION QUANTITY 
1    21    8400       25
2    21    7200       8
3    21    4300       5
4    21    3800       7
// assuming you have collected the results from your sql query like this:
while($r = $q->tech()) $dat[]=array($r['DIMENSION'],$r['QUANTITY']);

// you will end up with an array $dat: (dimension, quantity) like this:
// $dat=array(array(8400,25),array(7200,8),array(4300,5),array(3800,7));
// package size:
$n=10;

$cnt=0;                        // initialize package counter
$pack=array();                 // set up first package
$d=array_shift($dat);          // get first lot $d from $dat

do {     
  if ($d[1]>0){    
    $take=min($d[1],$n-$cnt);  // decide how much to take out ...
    $cnt+=$take;               // increase package counter
    $d[1]-=$take;$pack[]=array($d[0],$take); // tranfer the goods ...
                               // if package is full, start next one
    if ($cnt==$n) {$packs[]=$pack; $pack=array(); $cnt=0;}
  } else if(count($dat)>0){    // still elements in $dat left?
    $d=array_shift($dat);      // get the next lot $d from $dat
  } else break;    
} while(true);
if ($cnt>0) $packs[]=$pack;    // collect the last "stranded" package ...

echo str_replace("]],","]],\n ",json_encode($packs));    // as JSON string
// print_r($packs);            // conventional (bulky) PHP object notation
这将导致以下结果(JSON表示法):


你可以这样做:

ID ORDERID DIMENSION QUANTITY 
1    21    8400       25
2    21    7200       8
3    21    4300       5
4    21    3800       7
// assuming you have collected the results from your sql query like this:
while($r = $q->tech()) $dat[]=array($r['DIMENSION'],$r['QUANTITY']);

// you will end up with an array $dat: (dimension, quantity) like this:
// $dat=array(array(8400,25),array(7200,8),array(4300,5),array(3800,7));
// package size:
$n=10;

$cnt=0;                        // initialize package counter
$pack=array();                 // set up first package
$d=array_shift($dat);          // get first lot $d from $dat

do {     
  if ($d[1]>0){    
    $take=min($d[1],$n-$cnt);  // decide how much to take out ...
    $cnt+=$take;               // increase package counter
    $d[1]-=$take;$pack[]=array($d[0],$take); // tranfer the goods ...
                               // if package is full, start next one
    if ($cnt==$n) {$packs[]=$pack; $pack=array(); $cnt=0;}
  } else if(count($dat)>0){    // still elements in $dat left?
    $d=array_shift($dat);      // get the next lot $d from $dat
  } else break;    
} while(true);
if ($cnt>0) $packs[]=$pack;    // collect the last "stranded" package ...

echo str_replace("]],","]],\n ",json_encode($packs));    // as JSON string
// print_r($packs);            // conventional (bulky) PHP object notation
这将导致以下结果(JSON表示法):


这正是我想要的。一旦我得到这些信息,我需要将其存储在数据库中。我的表格是有组织的ID | ORDERID | DIMENSION | QUANTITY | NAME。我有订单id、尺寸(8400)和数量10。对于第一套,订单名称可以是Packet1,对于第二套Packet2,对于第三种情况,两套的Packet3将是相同的名称8400和7200。它增加1。Idk,如果可能的话。谢谢,这正是我想要的。一旦我得到这些信息,我需要将其存储在数据库中。我的表格是有组织的ID | ORDERID | DIMENSION | QUANTITY | NAME。我有订单id、尺寸(8400)和数量10。对于第一套,订单名称可以是Packet1,对于第二套Packet2,对于第三种情况,两套的Packet3将是相同的名称8400和7200。它增加1。Idk,如果可能的话。非常感谢。