首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP循环更改表的单元格背景色

使用PHP循环更改表的单元格背景色
EN

Stack Overflow用户
提问于 2017-11-17 23:14:28
回答 2查看 894关注 0票数 0

我需要改变细胞的背景颜色与冻结温度(32F,0C)为#bff9ff,但有一些困难。我试图在<td>中打印CSS类,但似乎它在循环中不能正常工作,并且同时被打印。

然而,这是一半的问题。我如何识别那些冻结温度和以下的细胞不是手动而是使用PHP?

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-17 23:21:24

通过添加一个if语句来测试温度,然后将一个类添加到td标记中,这样就可以处理它了。

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2017-11-17 23:23:54

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

代码语言:javascript
复制
"<td class='$freezing'></td>"

基本评估如下:

代码语言:javascript
复制
if (32 <= $farenheit || 0 <= $celcius) { 
  $freezing = "freezing";
}

编辑: CSS

代码语言:javascript
复制
.freezing {
  background-color: #bff9ff;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47360649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档