1

さまざまな MySQL データのテーブル セルの背景色を変更したいと考えていました。私の状況では、ユーザーが体重と身長を入力すると、ボディマス指数 (BMI) が計算され、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;
}
4

2 に答える 2

3

$tdClass 変数を割り当てるコードは、$tdClass 変数を使用するループの後にあるため、td タグには適切なクラスがありません。これに変えて…

    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)) 
 {
    if ($row['Health_Measure'] == "Underweight") 
        $tdClass = 'underweight';

else if ($row['Health_Measure'] == "Normal Weight") 
    $tdClass = 'normalweight';

else if ($row['Health_Measure'] == "Overweight") 
    $tdClass = 'overweight';

 echo "<tr><td>" . $row['Name'] . "</td>"; 
 //some more codes for weight, height, BMI
 echo "<td class='<?php $tdClass; ?>'>" . $row['Health_Measure'] . "</td></tr>";
 }
}
echo "</table>";
于 2013-04-01T19:49:10.870 に答える
0

テーブル行を出力するループ内の TD の CSS クラスを決定するコードを移動するだけです。

于 2013-04-01T19:43:24.863 に答える