0

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?

3
  • What is the result you expect? Give some sample input and the corresponding output. Commented Jan 21, 2011 at 11:04
  • What do you mena by "difference between values"? Commented Jan 21, 2011 at 11:05
  • 1,3,6,8,12,45, this would be might raw mysql data but i would want 2,3,2,4,33...by the way i can do simple math but its just doing it within the while loop i'm struggling with Commented Jan 21, 2011 at 11:05

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

brilliant so, $newData is essentially my $row array?
$newData is essentially $row['steam'], not the entire $row array... you have other entries in $row such as $row["tstamp"]
oh ok. so what i would do is: value='".$newData."'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.