开发者

PHP work out difference between two values in while loop

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 开发者_Python百科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?


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.


$dataArray = explode(',',$row['steam']);
$dataCount = count($dataArray);
$newArray = array();
for($i = 1; $i < $dataCount; $i++) {
   $newArray[] = $dataArray[$i] - $dataArray[$i-1];
}
$newData = implode(',',$newArray);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜