2017-04-03 45 views
-1

初始化在CSS图象背景我有初始化变量从这个目录获取如图使用PHP可变

<?php $imgSrc = base_url()."decoy/thumbs/".$item["filename"]; ?> 

现在我想将变量到CSS代码块的背景URL指定为示出,但它是不显示图像背景

<div style="background: url('<?php echo $imgSrc ?>') no-repeat;" class="large"></div> 

任何人都可以亲切地帮助我如何我可以实现上述。

+4

你确定你的'$ imgSrc'是给你正确的网址是什么? – Samundra

+0

echo $ imgSrc – paranoid

+0

我做了类似的工作,它工作正常 >>>>> parker

回答

0

您需要将“imagePath”替换为“imgSrc”,以便图像在背景中提示,因为在backroud-url中不需要绝对路径,只有相对路径对此适用。

$imgPath = "../decoy/thumbs/".$item["filename"]"; 
<div style="background: url('<?php echo $imgPath; ?>') no-repeat;" class="large"></div> 
+0

请使用这个:<?php $ imgPath =“../decoy/thumbs/".$item["filename”];?> –

0

按照这些简单的步骤来调查您的问题。 获取图像

<?php $imgSrc = base_url()."decoy/thumbs/".$item["filename"]; ?> 

验证什么返回到图像变量<?php echo $imgSrc; ?> 你,如果你没有发现任何问题更新您的必要信息的问题最好放在这样之后。

最后,我更喜欢你使用字符串连接,而不是改变这种

<div style="background: url('<?php echo $imgSrc ?>') no-repeat;" class="large"></div> 

<?php 

    $imgSrc = base_url()."decoy/thumbs/".$item["filename"]; 
     echo '<div style="background:'.url($imgSrc).' no-repeat;" class="large"></div>'; 
    ?>