dongzhun8449 2018-07-21 07:09
浏览 94
已采纳

PHP:如何将记录从一个表复制到另一个表但是它必须复制到某个值

I am trying to fetch SUM(WeightKgs) column and sum up each field to <=2000. Once it gets close to 2000, it should then save it in the database table with a common ID (to fetch for future query). Once it saved in the database, it then continues to fetch the next row and sum up to <= 2000. This process is required to do till the last row.

I tried few while loops and for and for each loops but cant get my head around.

screenshot:

enter image description here

PHP code

       <?php

  $total = 0;
  $getValue = 0;
  $end = 2000;
   do { 
      $start = $row_GenRunRS['SUM(WeightKgs)'] ;
      $getValue = $start + $total;

      echo  $row_GenRunRS['SUM(WeightKgs)'].  " + ". $total ." = " . $getValue . " <br>";
      $total = $start + $total ;
      echo "<br> <hr><br>";
     // echo    " Start ". $start. " <br>";



  } while ($row_GenRunRS = mysql_fetch_assoc($GenRunRS)); 

  echo "Total now = " .$total;

  ?>

Outputenter image description here

  • 写回答

1条回答 默认 最新

  • dotws86260 2018-07-21 09:22
    关注

    Based on what you've asked for, the answer is something like this.
    However, still I'm not entirely sure I've understand what you need to do.

    <?php
            $total_weight = 0;    
            while($row_GenRunRS = mysql_fetch_assoc($GenRunRS)) {
    
                $total_weight = $row_GenRunRS['SUM(WeightKgs)'] + $total_weight ;
                echo "Total is: $total_weight <br><br>";   
    
                if($total_weight >= 2000) {
                    echo "This is what you have to save: $total_weight <br><br>";
                    $total_weight = 0;
                }
            }
    ?>
    



    Edit: This code will give you results close to 2000 but most of them will be greater than 2000. If you need to have all the values bellow 2000, then you have to save the total weight before you add the package which make it bigger than 2000.

    <?php
    
        $total_weight = 0;    
        do {
    
            if($row_GenRunRS['SUM(WeightKgs)'] + $total_weight >= 2000) {
                echo "This is what you have to save: $total_weight <br><br>";
                $total_weight = 0;
            }
    
            $total_weight = $row_GenRunRS['SUM(WeightKgs)'] + $total_weight ;
            echo "Total is: $total_weight <br><br>";   
    
        } while($row_GenRunRS = mysql_fetch_assoc($GenRunRS));
    
        echo "This is the last package that you also have to save: $total_weight <br><br>";
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题