简体   繁体   中英

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.

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

your syntax for the css is incorrect:

<div class="fill" style="background-image:url('<?php echo "getImg.php?id=$X13" ?>')"></div>

no <img src ...

Your background-image:url syntax and variable name are incorrect , try the folowing:

<?php
echo "<div class='fill' style=\"background-image:url('getImg.php?id=$VALIDVARIABLE')\"></div>";

Rules for PHP variables:

  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 _ )
  5. Variable names are case-sensitive ( $age and $AGE are two different variables)

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. You need to put the url to the image.

I hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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