[英]Changing table's cell background color using PHP while loop
我需要將冷凍溫度(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>
創建一個名為“ freezing”的CSS類。 使用if添加凍結類,例如,
"<td class='$freezing'></td>"
基本上評估一下:
if (32 <= $farenheit || 0 <= $celcius) {
$freezing = "freezing";
}
編輯:CSS
.freezing {
background-color: #bff9ff;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.