Taximaniac

Programming With GIO YT - Change by Christian Haugland

Feb 17th, 2021 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. <?php
  2. // this function was added into helpers.php
  3.  
  4. function getAmountColorStyle(float $amount): string
  5. {
  6.     $color;
  7.  
  8.     if($amount < 0) {
  9.         $color = 'red';
  10.     } elseif ($amount > 0) {
  11.         $color = 'green';
  12.     } else {
  13.         $color = '';
  14.     }
  15.  
  16.     return $color;
  17. }
  18.  
  19. // then in transactions.php file we add:
  20.  
  21. <span style="color: <?php echo getAmountColorStyle($transaction['amount']) ?>;" >
  22.     <?= formatDollarAmount($transaction['amount']) ?>
  23. </span>
  24.  
  25. // This way, we only need to have one span where we put out $transaction['amount']
  26. // and it will be styled with color-based if the number is negative or positive
Add Comment
Please, Sign In to add comment