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 关于#java#的问题,请各位专家解答!
  • ¥15 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常