2014-09-28 57 views
0

我想知道是否有人可以帮忙。我正在尝试将PHP代码(从数据库中检索图像)添加到CSS中。如何将PHP添加到CSS?

我在我的example.php页面上有一小段CSS代码,需要根据发布到example.php页面的变量来为背景设置不同的图像。

,我想出了可怕的代码如下

<style> 
    .par-surround { 
     background-repeat: no-repeat; 
     background-image:<?php <img src=<?=$subject['picture']?>border="0" width="740px"   height="auto"/> ?> ; 
     background-position: center top; 
    } 
    </style> 

我不知道这是可以做到,或者如果我必须这样做完全相反。任何帮助将不胜感激,谢谢。

+0

我不认为这是PHP的哟! – Charles 2014-09-28 00:23:03

+0

http://stackoverflow.com/questions/5338972/how-to-include-php-code-in-css – danronmoon 2014-09-28 00:23:13

+1

嗯...我会建议你学习PHP的最基本的 – 2014-09-28 00:23:20

回答

0

的reasodin是正确的。你只定义有一个有效的PHP代码:

<style> 
    .par-surround { 
     background-repeat: no-repeat; 
     background-image: url(<?php echo $subject['picture'] ?>); 
     background-position: center top; 
    } 
</style> 

还要确保$主题被定义并具有价值,你输出的CSS代码之前。

2

它可以完成,但你需要输出正确的CSS来做到这一点。 background-image不采取(无效)HTML图像标签:

<style> 
    .par-surround { 
     background-repeat: no-repeat; 
     background-image: url(<?=$subject['picture']?>); 
     background-position: center top; 
    } 
</style>