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 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题