How do I put a background image in CSS using PHP?

Asked

Viewed 88 times

-3

I’m trying to put a background image by css using PHP, which means the image I want to put in the background is inside the database. I open the php tags inside the background URL

 background-image:  url(?php echo $top['ft_feature']; ?>); 

The code I use for the bank to find the image is this:

  $consulta3 = "SELECT * FROM feature";
  $con3 = $mysqli->query($consulta3) or die (@mysqli_error());

And the code I put in

background: url()
like I said is this:

echo $top['ft_feature'];

I was wondering if there’s any way to put it as background using the image I selected from the bank.

  • When vc says css, vc means the css file or a style block within a php page?

1 answer

0


It has as yes Vinicius,

but remember that the php code should be inside a '.php' file to be interpreted by the server, if you are trying to put the php code inside the file'. css' will not work because the server will not interpret.

inside the '.php' file you can put php code inside your tag, like this:

<div style="background-image: url('<?php echo $top['ft_feature']; ?>')">

In this example I used the tag div you will put your tag.

Still inside the '.php' file you can also use your php code in a block of styles, thus:

<html>
    <head>
        <style>
        .exemplo {
            background-image: url("<?php echo $top['ft_feature']; ?>")
        }
        <style>
    </head>
    <body>
        <div class="exemplo">
        ...
        </div>
    </body>
</html>

*There is a syntax error in your question, missing a '<' in the php block opening and the block has to be in quotes following the css syntax: background-image: url(' ')

  • <body background="<? php echo $top['ft_feature']; ?>">

  • Hello, thank you so much for the reply, it worked perfectly, now when I add the poster link, it shows what is in the bank, thanks even

  • No need to thank Vinicius, just mark my answer as the correct one by clicking on the check (right sign) on the left side of the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.