gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:37:11 25 4
gpt4 key购买 nike

我需要将卡住温度(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>

最佳答案

通过添加一个 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>

关于php - 使用 PHP while 循环更改表格单元格背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47360649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com