[英]using background-image with php
I retrieve an image from the database with php and then I want to use this image in html
as a background. 我使用php从数据库中检索图像,然后将
html
中的图像用作背景。
I try to use this code, but it doesn't work: 我尝试使用此代码,但不起作用:
<div class="fill" style="background-image:url('<?php echo "<img src='getImg.php?id=$13'>"; ?>');"></div>
Rajdeep Paul is correct the variable is invalid also Rajdeep Paul是正确的变量也无效
your syntax for the css is incorrect: 您的CSS语法不正确:
<div class="fill" style="background-image:url('<?php echo "getImg.php?id=$X13" ?>')"></div>
no <img src
... 否
<img src
...
Your background-image:url
syntax and variable
name are incorrect , try the folowing: 您的
background-image:url
语法和variable
名称 不正确 ,请尝试以下操作:
<?php
echo "<div class='fill' style=\"background-image:url('getImg.php?id=$VALIDVARIABLE')\"></div>";
Rules for PHP variables: PHP变量规则:
$
sign, followed by the name of the variable $
符号开头,后跟变量名称 Az
, 0-9
, and _
) Az
, 0-9
和_
) $age
and $AGE
are two different variables) $age
和$AGE
是两个不同的变量) Try this: 尝试这个:
<?php $url = require("path/to/getImg.php?id=".$13;); ?>
<div class="fill" style="background-image:url('<?php echo $url; ?>');"></div>
I am not sure about this line: 我不确定这条线:
<?php $url = require("path/to/getImg.php?id=".$13;); ?>
But the value what you need to use on the background-image:url('anything') is not a img tag... sure. 但是,您需要在background-image:url('anything')上使用的值不是img标签...当然。 You need to put the url to the image.
您需要将URL放入图像。
I hope it helps you. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.