PHP/MySql:循环查询以插入行,并在重量上添加总和(例如最大200公斤),然后继续下一行
我正在尝试向新表中添加行 我使用foreach循环来插入数据,并且我能够插入数据。我也能够达到极限,并添加一个新行。但问题是一旦循环结束,它会添加单独的行,而不会添加到我想要的限制 我不太确定如何在这里添加表格。但请查找以下代码:PHP/MySql:循环查询以插入行,并在重量上添加总和(例如最大200公斤),然后继续下一行,php,mysql,laravel,loops,Php,Mysql,Laravel,Loops,我正在尝试向新表中添加行 我使用foreach循环来插入数据,并且我能够插入数据。我也能够达到极限,并添加一个新行。但问题是一旦循环结束,它会添加单独的行,而不会添加到我想要的限制 我不太确定如何在这里添加表格。但请查找以下代码: $fetchRecordRS = DB::select(" Select `ID` , `DeliveryAddress` , `Postzone` , `CarrierID` , `DeliveryInstruction`
$fetchRecordRS = DB::select("
Select `ID`
, `DeliveryAddress`
, `Postzone`
, `CarrierID`
, `DeliveryInstruction`
, `JobID`
, `JobName`
, Quantity
, `Bundlesize`
, SUM(Bundles) as 'SumBundles'
, SUM(Items) as 'SumItems'
, WeightKgs
, TotalWeightKgs
, SUM(WeightKgs) AS 'FixWeight'
, Notes
FROM UpdatedCsvFiles
WHERE DeliveryAddress != 'PLEASE LEAVE IN WAREHOUSE'
AND JobID != 0
GROUP
BY DeliveryAddress
, JobName
, CarrierID
ORDER
BY CarrierID
, DeliveryAddress ASC
");
foreach ($fetchRecordRS as $row_FetchRecordRS) {
$id = $row_FetchRecordRS->ID;
$carr_ID = $row_FetchRecordRS->CarrierID;
$address = $row_FetchRecordRS->DeliveryAddress;
$potzone = $row_FetchRecordRS->Postzone;
$instruction = $row_FetchRecordRS->DeliveryInstruction;
$quantity = $row_FetchRecordRS->Quantity;
$jobID = $row_FetchRecordRS->JobID;
$jobName = $row_FetchRecordRS->JobName;
$bundlesize = $row_FetchRecordRS->Bundlesize;
$bundle = $row_FetchRecordRS->SumBundles;
$items = $row_FetchRecordRS->SumItems;
//$num = $row_FetchRecordRS->FixWeight;
//$suburb = $row_FetchRecordRS->Suburb;
//$row_FetchRecordRS->TotalWeightKgs
$num = $row_FetchRecordRS->TotalWeightKgs;
$notes = $row_FetchRecordRS->Notes;
$num = $row_FetchRecordRS->FixWeight;
if ($num + $total > 200) {
echo ($num + $total) . "in Van " . $i ;
$sqltransitlist = DB::select("Insert INTO TransitList(genID, total) Values ('$i','$total')");
// loop A
foreach ($arr as $data) {
// echo "<br> first loop: " . $i;
$sqlquerytest = DB::select("Insert INTO GenerateRun(CarrierID, DeliveryAddress, Postzone, DeliveryInstruction, Quantity, JobID,
JobName, Notes, Bundlesize, Bundles, Items, FinalWeight, LodingZoneID)
VALUES('" . $data['CarrierID'] ."','" . $data['DeliveryAddress'] ."','" . $data['Postzone'] .
"','" . $data['DeliveryInstruction']. "','" .$data['Quantity'] . "','" . $data['JobID'] .
"','" . $data['JobName'] . "','" .$data['Notes'] . "','" . $data['Bundlesize']. "','" .$data['Bundles'] .
"','" . $data['Items'] ."','" .$data['FixWeight']."','" .$i ."')");
}
$arr = array();
$i++;
$total = 0;
}
$total += $num;
$arr[] = array('CarrierID' => $carr_ID, 'DeliveryAddress' => $address, 'Postzone' => $potzone,
'DeliveryInstruction' => $instruction, 'Notes' => $notes, 'Quantity' => $quantity, 'JobID' => $jobID,
'JobName' => $jobName, 'Bundlesize' => $bundlesize, 'Bundles' => $bundle, 'Items' => $items,
'FixWeight' => $num);
}
if ($total > 0) {
$sqltransitlist = DB::select("Insert INTO TransitList(genID, total) Values ('$i','$total')");
// loop B
foreach ($arr as $data) {
// echo "<br> Second loop: " . $i;
$sqlquerytest = DB::select("Insert INTO GenerateRun(CarrierID, DeliveryAddress, Postzone, DeliveryInstruction, Quantity, JobID,
JobName, Notes, Bundlesize, Bundles, Items, FinalWeight, LodingZoneID)
VALUES('" . $data['CarrierID'] ."','" . $data['DeliveryAddress'] ."','" . $data['Postzone'] .
"','" . $data['DeliveryInstruction']. "','" .$data['Quantity'] . "','" . $data['JobID'] .
"','" . $data['JobName'] . "','" .$data['Notes'] . "','" . $data['Bundlesize']. "','" .$data['Bundles'] .
"','" . $data['Items'] ."','" .$data['FixWeight']."','" .$i ."')");
$i++;
}
}
$fetchRecordRS=DB::select(“
选择'ID'`
,`送货地址`
,`邮政区`
“卡里里里德`
,`交货指示`
,`JobID`
,`JobName`
量
,`Bundlesize`
,总和(束)为“SumBundles”
,总和(项目)为“总和项目”
,重量公斤
,总重量公斤
,总和(重量千克)为“固定重量”
,注释
从更新的CSVFiles
何处交货地址!=“请留在仓库”
和JobID!=0
团体
按送货地址
,工作名称
,CarrierID
命令
卡里里德
,送货地址ASC
");
foreach($fetchrecords作为$row_fetchrecords){
$id=$row\u fetchrecorders->id;
$carr\u ID=$row\u fetchrecorders->CarrierID;
$address=$row\u fetchrecorders->DeliveryAddress;
$potzone=$row\u fetchrecorders->Postzone;
$instruction=$row\u fetchrecorders->DeliveryInstruction;
$quantity=$row\u fetchrecorders->quantity;
$jobID=$row\u fetchrecorders->jobID;
$jobName=$row\u fetchrecorders->jobName;
$bundlesize=$row\u fetchrecorders->bundlesize;
$bundle=$row\u fetchrecorders->SumBundles;
$items=$row\u fetchrecorders->sumtitems;
//$num=$row\u fetchrecorders->FixWeight;
//$郊区=$row\U获取记录器->郊区;
//$row_fetchrecorders->TotalWeightKgs
$num=$row\u fetchrecorders->TotalWeightKgs;
$notes=$row\u fetchrecorders->notes;
$num=$row\u fetchrecorders->FixWeight;
如果($num+$total>200){
echo($num+$total)。“货车内”。$i;
$sqltransitlist=DB::select(“插入TransitList(genID,total)值(“$i”,“$total”)”;
//环路A
foreach($arr作为$data){
//echo“
第一个循环:”.$i;
$sqlquerytest=DB::select(“插入到生成器中(CarrierID、DeliveryAddress、Postzone、DeliveryInstruction、Quantity、JobID、,
作业名称、备注、捆绑大小、捆绑、项目、最终重量、LodingZoneID)
值(““$data['CarrierID']”、“$data['DeliveryAddress']”、“.”、“$data['Postzone']”)。
“,”,“$data['DeliveryInstruction']。”,“,”,“$data['Quantity']。”,“,”,“$data['JobID']。
“,”、“$data['JobName']..”、“$data['Notes'.]”、“$data['Bundlesize'.]”、“,”、“$data['Bundles'.]”。
“,”、“$data['Items']。”、“$data['FixWeight']。”、“$i.”);
}
$arr=array();
$i++;
$total=0;
}
$total+=$num;
$arr[]=array('CarrierID'=>$carr\u ID,'DeliveryAddress'=>$address,'Postzone'=>$potzone,
'DeliveryInstruction'=>$instruction',Notes'=>$Notes',Quantity'=>$Quantity',JobID'=>$JobID,
'JobName'=>$JobName','Bundlesize'=>$Bundlesize',Bundles'=>$bundle',Items'=>$Items,
“固定重量”=>$num);
}
如果($total>0){
$sqltransitlist=DB::select(“插入TransitList(genID,total)值(“$i”,“$total”)”;
//回路B
foreach($arr作为$data){
//回声“
第二个循环:”.$i;
$sqlquerytest=DB::select(“插入到生成器中(CarrierID、DeliveryAddress、Postzone、DeliveryInstruction、Quantity、JobID、,
作业名称、备注、捆绑大小、捆绑、项目、最终重量、LodingZoneID)
值(““$data['CarrierID']”、“$data['DeliveryAddress']”、“.”、“$data['Postzone']”)。
“,”,“$data['DeliveryInstruction']。”,“,”,“$data['Quantity']。”,“,”,“$data['JobID']。
“,”、“$data['JobName']..”、“$data['Notes'.]”、“$data['Bundlesize'.]”、“,”、“$data['Bundles'.]”。
“,”、“$data['Items']。”、“$data['FixWeight']。”、“$i.”);
$i++;
}
}
“它添加单个行,而不添加到我想要的限制。”。。。。。您希望代码中的限制是什么?$arr
只有一个元素,这就是为什么foreach($arr as$data)
只执行了一次,我想您希望循环A
中的$arr[]=array('C……。
中的行A
添加多个元素。请参阅关于sql注入以及准备好的和绑定查询的重要性“它添加单个行,而不添加到我想要的限制。”……您想要在代码中设置什么限制?$arr
只有一个元素,这就是为什么foreach($arr as$data)
只执行了一次,我想您需要该行$arr[]=array('C……。
在循环A
中添加多个元素。请参阅关于sql注入以及准备好的和绑定的查询的重要性