0

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

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. 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. Commented Mar 24 at 19:33

1 Answer 1

2

You are displaying the value inside the loop before updating $prev_row. you should:

  1. Store the results in an array first.

  2. 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>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much, the only problem is that I have the same result with the order being the same. The dash is at the top like before. I made minor changes to your code and provided more of my original code that I left out so that it wouldn't look so messy. Here is the full code for that section 3v4l.org/B7RQ7
Here is the very first value of the array when you print_r Array ( [0] => Array ( [change] => - [change_pay] => 2267.79 [funds_owned] => test [gain] => -8558.58 [change_qty] => 2400.7378 [change_cost_basis] => 53778.04 [change_date] => 1742582868 ) You can see the very first change value is the dash. The change column needs to moved up one while the other values need to remain

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.