根据值更改文本颜色

时间:2021-03-17 19:59:12

标签: php

我只是想根据它们的值以红色或绿色显示结果。阴性或阳性。 我有以下代码,但它不起作用。任何人都可以帮忙吗?

<?php
            $total = 0;
            $index = 0;
                foreach($products as $product){
                    $index ++;
                    ?>
                    
                    <tr>
                    <?php
                        $style = ($total < 0) ? "color:red; font-weight:bold;" : "color:green; font-weight:bold;";
                    ?>
                    
                        <td class="text-center" style="<?php echo $style ?>" >
                        <?php
                            $total_item = $product['quantity'] * $product['sale_price'];
                            $total += $total_item;
                            if ($index == count($products)) {
                                
                            echo formatcurrency ($total);
                            }
                        ?>
                        </td>
                    </tr>
                <?php
                }
        ?>

enter image description here

我的原始代码在这里,但我只想显示最后一个值,所以我修改了上面的代码。现在我需要显示为红色如果是负数或绿色如果是正数。

<table>
      <?php
       $total = 0;
       foreach($products as $product){  
       ?>
       <tr>
           <td>...</td>
           <td>...</td>
           <td>
      <?php
        $total_item = $product['quantity'] * $product['sale_price'];
        $total += $total_item;
        echo formatcurrency($total);  
      ?>
        </td>
      </tr>
  <?php
  }
  ?>
... ... $-225.00 ... ... $-755.00 ... ... $-665.00

0 个答案:

没有答案
相关问题