我需要在HTML中的MYSQL数据库值上使用css样式。在我的素食数据库行中,"Y"表示"是","N"表示"否"。我需要将css应用于'Y'和'N'值,这样我就可以用css为它们设置样式了?我正在使用PHP来显示我的页面。
if ($result1->num_rows > 0) {
// output data of each row
while($row = $result1->fetch_assoc()) {
echo '<h3 class="auto-title"> ' , $row["food"] , ' </h3>' , $row["description"] , '</br>
<div class="vegetarian"> ' , $row["vegetarian"] , ' </div>
<b class="auto-price"> ' , $row["price"] , ' </b><br> ';
}
}
我试过在下面使用这个CSS,但没有乐趣。
div.vegetarian [value="Y"] {
color: red;
}
您可以使用if条件轻松实现
CSS:
vegetarian-y {
color: red;
}
vegetarian-n {
color: green;
}
php:
if ($result1->num_rows > 0) {
// output data of each row
while($row = $result1->fetch_assoc()) {
$class = "vegetarian-n";
if($row["vegetarian"] == "Y")
$class = "vegetarian-y";
echo '<h3 class="auto-title"> ' , $row["food"] , ' </h3>' , $row["description"] , '</br>
<div class="'.$class.'"> ' , $row["vegetarian"] , ' </div>
<b class="auto-price"> ' , $row["price"] , ' </b><br> ';
}
}
那么接下来会发生什么呢,它每次都会检查,如果素食列的值是Y,那么它将应用素食-Y类,否则它将应用纯素食-n类