我想为不同的 MySQL 数据更改表格单元格的背景颜色。我的情况是当用户输入他们的体重和身高时,它会计算他们的体重指数(BMI)并输出BMI类别。像这样的东西:
现在如何更改 BMI 类别的表格单元格颜色,其中“体重过轻”为白色,“正常体重”为黄色,“超重”为橙色?我尝试了以下但不起作用。
这就是我的 PHP 代码中的内容:
echo "<table border=\"1\"><tr><th>Name</th> //etc.
if (mysqli_num_rows($result) == 0)
echo "<tr><td colspan='2'>No records found.</td></tr>";
else {
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>" . $row['Name'] . "</td>";
//some more codes for weight, height, BMI
echo "<td class='<?php $tdClass; ?>'>" . $row['Health_Measure'] . "</td>";
}
}
echo "</table>";
if ($row['Health_Measure'] == "Underweight")
$tdClass = 'underweight';
else if ($row['Health_Measure'] == "Normal Weight")
$tdClass = 'normalweight';
else if ($row['Health_Measure'] == "Overweight")
$tdClass = 'overweight';
CSS:
.underweight {
background-color:white;
}
.normalweight {
background-color:yellow;
}
.overweight {
background-color:orange;
}