简体   繁体   English

用PHP使用背景图像

[英]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变量规则:

  1. A variable starts with the $ sign, followed by the name of the variable 变量以$符号开头,后跟变量名称
  2. A variable name must start with a letter or the underscore character 变量名称必须以字母或下划线字符开头
  3. A variable name cannot start with a number 变量名称不能以数字开头
  4. A variable name can only contain alpha-numeric characters and underscores ( Az , 0-9 , and _ ) 变量名称只能包含字母数字字符和下划线( Az0-9_
  5. Variable names are case-sensitive ( $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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM