douyi1855 2018-07-25 02:29
浏览 89
已采纳

如何获取记录总和为一个值,然后添加到两个新表?

I am trying to sum (add the values) from the database. My application checks the values from each row, adds up the value from each row up to 2000. And once it reaches up to 2000, it saves in the database (insert query) and continues the same till last record fetched. The total value summed (or totaled) up by each rows should not exceed over 2000.

There are two insert queries, One for inserting the total( from each row between 1800 and 2000) with the ID (like Primary key) generated and the second table add each row inserted with ID (the ID generated becomes now foreign key)

Please refer to the screenshot.

enter image description here Please find the code below:

$i = 1;
do {                
    $id = $row_FetchRecordRS['ID'];
    $dateissued = $row_FetchRecordRS['DateIssued'];
    $rundateCarrierRun = $row_FetchRecordRS['RundateCarrierRunID'];
    $timegenerated = $row_FetchRecordRS['TimeGenerated'];
    $carrierID = $row_FetchRecordRS['CarrierRunID'] ;
    $areaID = $row_FetchRecordRS['CarrierAreaID'];
    $address = $row_FetchRecordRS['DeliveryAddress'];
    $potzone = $row_FetchRecordRS['Postzone'];
    $carr_ID = $row_FetchRecordRS['CarrierID'];
    $instruction = $row_FetchRecordRS['DeliveryAddress'];
    $areaRep =  $row_FetchRecordRS['AreaRepDetails'];
    // $vendor = $row_FetchRecordRS['VendorDetails'];
    $quantity = $row_FetchRecordRS['Quantity'];
    $direct = $row_FetchRecordRS['Direct'];
    $jobID = $row_FetchRecordRS['JobID'];
    $jobName = $row_FetchRecordRS['JobName'];
    $bundlesize = $row_FetchRecordRS['Bundlesize'];
    $bundle = $row_FetchRecordRS['Bundles'];
    $items = $row_FetchRecordRS['Items'];
    $weight = $row_FetchRecordRS['WeightKgs'];
    $totalWeightCol = $row_FetchRecordRS['TotalWeightKgs'];
    $date = date("D M d, Y G:i");

    $total_weight =  $row_FetchRecordRS['FinalWeight'] + $total_weight ;
    echo "Row: " .$row_FetchRecordRS['FinalWeight']. "<br>";
    echo "Total is______  $i : $total_weight <br><br>";  

    $sqlquerytest = "INSERT INTO `GenerateRun`
        (`DateIssued`, `RundateCarrierRunID`, `TimeGenerated`, 
        `CarrierRunID`, `CarrierAreaID`, `DeliveryAddress`, `Postzone`, 
        `CarrierID`, `DeliveryInstruction`, `AreaRepDetails`,  
        `Quantity`, `Direct`, `JobID`, `JobName`, `Bundlesize`,   
        `Bundles`, `Items`, `WeightKgs`, `TotalWeightKgs`,   
        `LodingZoneID`) 
        VALUES 
        ('$dateissued', '$rundateCarrierRun', '$timegenerated',  
         '$carrierID', '$areaID', '$address', '$potzone', '$carr_ID',  
         '$instruction', '$areaRep', '$quantity', '$direct', '$jobID',  
         '$jobName', '$bundlesize', '$bundle', '$items', '$weight',   
        '$totalWeightCol','$i')";

    mysql_select_db($database_callmtlc_SalmatDB, $callmtlc_SalmatDB);
    $ResultUpd1 = mysql_query($sqlquerytest, $callmtlc_SalmatDB) or die(mysql_error()); 

    if ($total_weight >= 1800) {            
        $sqltransitlist = " INSERT INTO `TransitList`(`genID`, `total`) Values ('$i','$total_weight')";
        mysql_select_db($database_callmtlc_SalmatDB, $callmtlc_SalmatDB);
        $ResultUpd3 = mysql_query($sqltransitlist, $callmtlc_SalmatDB) or die(mysql_error());

        $i = $i+1;
        $total_weight = 0;          
    }
} while($row_FetchRecordRS = mysql_fetch_assoc($FetchRecordRS));
  • 写回答

1条回答 默认 最新

  • donljt2606 2018-07-25 03:34
    关注

    Some of your line of codes are irrelevant to your problem. I will simplify it for you.

    $i = 1;
    $total = 0;
    $arr = array(); // for storing a list of data provides that the total doesn't exceed 2000
    while ($row = mysql_fetch_assoc($record)) {
        $id = $row['id'];
        $name = $row['name'];
        $num = $row['num'];
        $arr[] = array('id' => $id, 'name' => $name, 'num' => $num);
        if ($num + $total > 2000) {
            $sql = "INSERT INTO Table1(genID, total) Values ('$i','$total')";
            mysql_query($sql) or die(mysql_error());
            foreach ($arr as $data) {
                $sql = "INSERT INTO Table2(ID, name, genID, total) Values ('$data[id]','$data[name]','$i','$data[num]')";
                mysql_query($sql) or die(mysql_error());
            }
            $arr = array(); // empty the array as the data has been stored to database
            $i++;
            $total = 0;   
        } else { // if the total doesn't exceed 2000, add it to total
            $total += $num;
        }
    }
    $sql = "INSERT INTO Table1(genID, total) Values ('$i','$total')";
    mysql_query($sql) or die(mysql_error());
    foreach ($arr as $data) {
        $sql = "INSERT INTO Table2(ID, name, genID, total) Values ('$data[id]','$data[name]','$i','$data[num]')";
        mysql_query($sql) or die(mysql_error());
    }
    

    Note: this code is just a sample, not your actual code. You can implement my code and match it to your code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)