如何在样式标签中使用PHP函数


how to use php function within style tag

我想在样式标签中动态添加背景图像并使用php代码返回div。所以我使用我的一般逻辑。但它不起作用。我编写代码如下

.CSS

<style>
.topadd{
background-image: url('../add/<?php echo $add?>');
width:100%;
height:auto;
}
</style>
</head>

.PHP

<?php
    $query="SELECT img FROM addbar where position='logo'";
    $result=$con->query($query) or die($con->error);
    while($row=$result->fetch_assoc()){
        $add=$row['img'];       
        if($add!=""){
            echo "<div class='topadd'></div>";
        }   
    }
?>

我知道 head 中包含的样式标签和 php 代码之间没有联系。我不能在 css 中使用以 html 形式返回变量的 php 函数。或者有什么想法来解决这个问题?

你可以用 .CSS

<style>
.topadd{
width:100%;
height:auto;
}
</style>
</head>

.PHP

<?php
$query="SELECT img FROM addbar where position='logo'";
$result=$con->query($query) or die($con->error);
while($row=$result->fetch_assoc()){
    $add=$row['img'];       
    if($add!=""){
          echo "<div class='topadd' style='background-image: url(../add/'".$add.")'></div>";
    }   
}
?>

希望对您有所帮助