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
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
Share Improve this question edited Mar 22 at 18:49 Shadow 34.3k10 gold badges65 silver badges75 bronze badges asked Mar 22 at 17:09 user26136009user26136009 291 silver badge8 bronze badges 2- If you want to do it with one loop, then you have to turn the logic around. You can not output the data for the current row within your loop, because you need data from the next row. So fetch the first row before your loop, and then inside your loop, use that data of the previous row and the current one to do your calculation, and then output the value from the previous row, plus the increase you have calculated. That'll leave you with one last row you'll have to output after your loop then. – C3roe Commented Mar 24 at 8:59
- Thanks, that makes sense but not sure how I would code that and make sure that they line up correctly. I am not a programmer, just tinker with my own website that no one else uses but me. That way I have my own layout just the way I like without having a third party constantly change things around, making the site slower, charging subscriptions, ads, etc. – user26136009 Commented Mar 24 at 19:33
1 Answer
Reset to default 2You are displaying the value inside the loop before updating $prev_row. you should:
Store the results in an array first.
Loop through the array to display them properly.
Hope it will work. I have pasted your updated code snippet below
$rows = []; // Store rows before displaying
$prev_row = null;
while ($change_row = mysql_fetch_array($result)) {
$change = '-'; // Default for the first row
if ($prev_row !== null) {
if ($prev_row > $change_row['change_pay']) {
$change_diff = (($prev_row - $change_row['change_pay']) / $prev_row) * 100;
$change = '<font color="red">' . number_format($change_diff, 2) . '% decrease</font>';
} else {
$change_diff = (($change_row['change_pay'] - $prev_row) / $prev_row) * 100;
$change = '<font color="green">' . number_format($change_diff, 2) . '% increase</font>';
}
}
// Store data in array instead of printing immediately
$rows[] = [
'pay' => $change_row['change_pay'],
'change' => $change
];
$prev_row = $change_row['change_pay'];
}
// Now loop through the stored data to display it correctly
foreach ($rows as $row) {
echo '<tr>
<td class="bigtext"><b>' . $row['pay'] . '</b></td>
<td class="bigtext"><b>' . $row['change'] . '</b></td>
</tr>';
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744306617a4567744.html
评论列表(0条)