I am trying to insert the data to the database by fetching them first, doing some additions and then setting a condition in the loop, if the value exceeds over > 2200. Inside this if condition, I have a for each loop where it will take all the records fetched and insert into the 2nd table. I am getting it right so far, now the problem is the remaining value from the table fetched, does not insert into the new tables. Please find the screenshot attached (yellow cells). I want to also make them save and inserted in both the tables and assign a value to it.
Code
if (isset($_POST["genRun"])) {
$total_weight = 0;
$i = 1;
$arr = array();
$excess = 0;
mysql_select_db($database_callmtlc_SalmatDB, $callmtlc_SalmatDB);
while($row_FetchRecordRS = mysql_fetch_assoc($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['Bundles'];
$items = $row_FetchRecordRS['Items'];
$weight = $row_FetchRecordRS['WeightKgs'];
$suburb = $row_FetchRecordRS['Suburb'];
$num = $row_FetchRecordRS['TotalWeightKgs'];
//$num = $row_FetchRecordRS['TotalWeightKgs'];
$arr[] = array('CarrierID' => $carr_ID, 'DeliveryAddress' => $address, 'Postzone' => $potzone, 'DeliveryInstruction' => $instruction, 'Quantity' => $quantity, 'JobID' => $jobID, 'JobName' => $jobName, 'Bundlesize' => $bundlesize, 'Bundles' => $bundle, 'Items' => $items, 'WeightKgs' => $weight, 'Suburb' => $suburb, 'TotalWeightKgs' => $num);
if ($num + $total > 2200) {
$sqltransitlist = "INSERT INTO TransitList(genID, total) Values ('$i','$total')";
$ResultUpd3 = mysql_query($sqltransitlist, $callmtlc_SalmatDB);
foreach ($arr as $data) {
$sqlquerytest = "INSERT INTO GenerateRun(CarrierID, DeliveryAddress, Postzone,DeliveryInstruction, Quantity, JobID, JobName,
Bundlesize, Bundles, Items, WeightKgs, Suburb, TotalWeightKgs,LodingZoneID) VALUES('"
. $data['CarrierID'] ."','" . $data['DeliveryAddress'] ."','" . $data['Postzone'] . "','" . $data['DeliveryInstruction']. "','" .$data['Quantity'] . "','" . $data['JobID'] . "','" . $data['JobName'] . "','" . $data['Bundlesize']. "','" .$data['Bundles'] . "','" . $data['Items'] . "','" .$data['WeightKgs']. "','" . $data['Suburb']."','" .$num."','" .$i ."')";
$ResultUpd1 = mysql_query($sqlquerytest, $callmtlc_SalmatDB);
}
$arr = array();
$i++;
$total = 0;
} else {
$total += $num;
}
}
// after the loop check if there are some data was not inserted and insert it after the loop is over
if ($total > 0) {
$sqltransitlist = "INSERT INTO TransitList(genID, total) Values ('$i','$total')";
$ResultUpd3 = mysql_query($sqltransitlist, $callmtlc_SalmatDB);
foreach ($arr as $data) {
$sqlquerytest = "INSERT INTO GenerateRun(CarrierID, DeliveryAddress, Postzone,DeliveryInstruction, Quantity, JobID, JobName,
Bundlesize, Bundles, Items, WeightKgs, Suburb, TotalWeightKgs,LodingZoneID) VALUES('"
. $data['CarrierID'] ."','" . $data['DeliveryAddress'] ."','" . $data['Postzone'] . "','" . $data['DeliveryInstruction']. "','" .$data['Quantity'] . "','" . $data['JobID'] . "','" . $data['JobName'] . "','" . $data['Bundlesize']. "','" .$data['Bundles'] . "','" . $data['Items'] . "','" .$data['WeightKgs']. "','" . $data['Suburb']."','" .$num."','" .$i ."')";
echo "$i , $total <br>";
$ResultUpd1 = mysql_query($sqlquerytest, $callmtlc_SalmatDB);
}
}
Output which I am getting it now: (When we add these total, the total is less than the required output)
Required Output: (when we do the loop < 2200, its not saving the value over 2200)
SQL" and thats the sql :
SELECT UpdatedCsvFiles.ID, UpdatedCsvFiles.CarrierID, UpdatedCsvFiles.DeliveryAddress, UpdatedCsvFiles.Postzone, UpdatedCsvFiles.DeliveryInstruction, UpdatedCsvFiles.Quantity, UpdatedCsvFiles.JobID, UpdatedCsvFiles.JobName, UpdatedCsvFiles.Bundlesize, UpdatedCsvFiles.Bundles, UpdatedCsvFiles.Items, UpdatedCsvFiles.WeightKgs, SuburbPostZone.Suburb, UpdatedCsvFiles.TotalWeightKgs FROM UpdatedCsvFiles LEFT JOIN SuburbPostZone on SuburbPostZone.areaID = UpdatedCsvFiles.CarrierID Where UpdatedCsvFiles.DeliveryAddress != 'PLEASE LEAVE IN WAREHOUSE' GROUP by UpdatedCsvFiles.CarrierID, UpdatedCsvFiles.DeliveryAddress ORDER by UpdatedCsvFiles.CarrierID , SuburbPostZone.Suburb, UpdatedCsvFiles.ID ASC