dtu1747 2011-01-21 11:00
浏览 37
已采纳

PHP计算while循环中两个值之间的差异

I'm outputting an XML document with PHP from mysql results, in one column I have a bunch of values, but these are accumulative values, i.e.: 1,3,6,8,12,45,etc...but how can I only echo out the difference between values rather than the values themselves in this:

while($row = mysql_fetch_array($result)){
    $strXML .= "<set name='".date("G:i:s", strtotime($row["tstamp"]))
            .  "' value='".$row['steam']."' color='AFD8F8' />";
}

The column in question is "steam", how could I go about doing this?

  • 写回答

2条回答 默认 最新

  • douzhou7124 2011-01-21 11:09
    关注

    Do you want to do this?

    $previous = 0;
    while($row = mysql_fetch_array($result)){
        $difference = $row['steam'] - $previous;
        $strXML .= "<set name='".date("G:i:s", strtotime($row["tstamp"]))
                .  "' value='".$difference."' color='AFD8F8' />";
        $previous = $row['steam'];
    }
    

    assuming $row['steam'] is a single number, not a comma separated list.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答