【问题标题】:Changing table's cell background color using PHP while loop使用 PHP while 循环更改表格单元格背景颜色
【发布时间】:2017-11-18 07:33:07
【问题描述】:

我需要将具有冷冻温度(32F,0C)的单元格的背景颜色更改为#bff9ff,但有一些困难。我试图在 <td> 中打印 CSS 类,但似乎它在循环内无法正常工作并同时被打印。

但是,这只是一个问题。我怎样才能识别那些温度低于冻结温度的细胞而不是手动而是使用PHP?

<html>
<head>
    <meta charset="UTF-8">
    <title>Unit 3 part 2</title>

        <style>
            table {
                font-family: arial, sans-serif;
                border-collapse: collapse;
                width: 100%;
            }

            tr:hover {
                background-color:#bff9ff;
                }

            td, th {
                border: 1px solid #dddddd;
                text-align: left;
                padding: 8px;``
            }
            .cell {
                background-color: #00bfff;
                }    

        </style>

</head>
<body>

    <table border="1" cellpadding="3">

        <thead>
            <th>Fahrenheit</th>
            <th>Celsius</th>
        </thead>

        <?php
        $fahrenheit = 50;

        while ($fahrenheit >= -50) {

            $celsius = ($fahrenheit - 32) * 5 / 9;

            print "<tr><td>$fahrenheit</td><td>$celsius</td></tr>";

            $fahrenheit -= 5;
            $celsius -= 5;



        } ?>

    </table>

</body>
</html>

【问题讨论】:

    标签: php css loops while-loop html-table


    【解决方案1】:

    通过添加一个 if 语句来测试温度,然后在 td 标签中添加一个类,这应该会处理它。

    <html>
    <head>
        <meta charset="UTF-8">
        <title>Unit 3 part 2</title>
        <style>
            table {
                font-family: arial, sans-serif;
                border-collapse: collapse;
                width: 100%;
            }
            tr:hover {
                background-color:#bff9ff;
                }
            td, th {
                border: 1px solid #dddddd;
                text-align: left;
                padding: 8px;``
            }
            .cell {
                background-color: #00bfff;
                }
            .cell.freezing {
                background-color: #bff9ff;
            }
        </style>
    </head>
    <body>
        <table border="1" cellpadding="3">
            <thead>
                <th>Fahrenheit</th>
                <th>Celsius</th>
            </thead>
            <?php
            $fahrenheit = 50;
            while ($fahrenheit >= -50) {
                $celsius = ($fahrenheit - 32) * 5 / 9;
                $class = '';
                if($fahrenheit <= 32) {
                    $class = ' freezing';
                }
                print "<tr><td class='cell $class'>$fahrenheit</td><td class='cell $class'>$celsius</td></tr>";
                $fahrenheit -= 5;
                $celsius -= 5;
            } ?>
        </table>
    </body>
    </html>
    

    【讨论】:

    • 很高兴它为您工作,请接受答案。谢谢!
    【解决方案2】:

    创建一个名为“freezing”的 CSS 类。使用 if 添加冻结类,例如,

    "<td class='$freezing'></td>"
    

    基本上评价这个:

    if (32 <= $farenheit || 0 <= $celcius) { 
      $freezing = "freezing";
    }
    

    编辑:CSS

    .freezing {
      background-color: #bff9ff;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 2013-04-12
      • 2021-10-31
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      相关资源
      最近更新 更多