The following code is simple and works when trying to compare previous values from one column to the next. The only problem is that it is displayed one row below where I want. So the numbers do not line up such as:
$300 -
$100 - 200% increase
$50 - 100% increase
So it looks like its showing that there was a 200% increase between $50 and $100 when that should be pushed up one row so that the 200% is from $100 to $300. I would like it to display like:
$300 - 200% increase
$100 - 100% increase
$50 -
This is the code I am using:
I added this inside of the while loop:
while ($change_row = mysql_fetch_array($result)
{
if ($prev_row && ($prev_row > $change_row['change_pay']) )
{
// The percentage increase from 30 to 40 is:
// (40-30)/30 * 100 = 33%
$change_diff = (($prev_row-$change_row['change_pay'])/$change_row['change_pay'] * 100);
$change = '<font color="green">'. number_format($change_diff, 2).'%</font>';
}
elseif ( strlen($prev_row) < 2 )
{
$change = '-';
}
else
{
//The percentage decrease from 40 to 30 is:
// (40-30)/40 * 100 = 25%.
$change_diff = (($prev_row-$change_row['change_pay'])/$prev_row * 100);
$change = '<font color="red">'. number_format($change_diff, 2).'</font>';
}
$prev_row = $change_row['change_pay'];
}
the $change variable will be displayed inside the while loop that I didn't show in the code above next to the current value, like:
<td class="bigtext"> <b>'.$change.'</b> </td>
<td class="bigtext"> <b>'.$change_row['change_pay'].'</b> </td>
I am wondering how to modify this code so that it pushes the results up one row, or is it impossible and have to be rewritten? The only thing I can think of is somehow using the while loop for the mysql results from the DB and then placing the $change values in an array then using a foreach loop for the html rows? Or maybe you know a better way. Thanks