【问题标题】:Use CSS on database value - PHP framework在数据库值上使用 CSS - PHP 框架
【发布时间】:2021-01-10 12:09:52
【问题描述】:

我需要在 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;
}

【问题讨论】:

  • echo '&lt;div class="vegetarian ', ($row['vegetarian'] == 'Y' ? 'vegetarian-y' : 'vegetarian-n'), '"&gt; ...'…?

标签: php html css mysql database


【解决方案1】:

你可以很容易地使用 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 类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 2010-12-22
    • 1970-01-01
    相关资源
    最近更新 更多