Forum Moderators: coopster
As part of my data output for row 2, 3, 4, etc.., I want to present the difference of a value between the current row and the previous row.
for instance if the first row's value is 2 and the second row's value is 3... I want to echo 1.
How do I go about getting the value of the previous row... seems like it should be pretty straight forward, but I'm having a tough time even writing out the logic for it...
Thanks in advance
Russ
$previousRow = 0; //initiates value for preceeding row
while($row = mysql_fetch_assoc($result)){
$currentRow = $row['fieldName']; //gets current row
$difference = $currentRow - $previousRow; //gets difference between the two variables
echo "\$difference = $difference<br>\n";
$previousRow = $currentRow; //reassigns previous row
}
this is probably the easier way. alternative is to use mysql_data_seek().
Excerpt from my code is below to share.
$prev_mile = 0;
while($row = mysql_fetch_array($result, MYSQL_NUM)){
$curr_mile = $row[1];
$diff = $curr_mile-$prev_mile; if($diff==$row[1]){
$mpg="0";
} else {
$mpg = $diff/$row[2];
}
echo $mpg;
Thank you