drgdn82648 2017-11-17 15:14
浏览 235
已采纳

使用PHP while循环更改表格单元格背景颜色

I need to change the background color of cells with freezing temperature (32F, 0C) to #bff9ff but have some difficulties. I tried to print CSS class inside <td> but seems it just doesn't work properly inside the loop and being printed at the same time.

However, it's half a problem. How can I identify those cells with freezing temp and below not manually but using 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>

展开全部

  • 写回答

2条回答 默认 最新

  • doumo2501 2017-11-17 15:21
    关注

    By adding an if statement to test the temperature, and then adding a class to the td tags, that should take care of it.

    <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>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部