0

So I have some PHP stuff inside my html doc, and I'm fairly new to web stuff so I'm kinda confused. The code in question is:

   <style type="text/css">
            body{
                background: url(<?php include 'background.php';
                    echo "$selectedBg"; ?>) no-repeat center center fixed;
                background: url(images/1.png)
                background-size: cover;
            }
    </style>

The rest of the file works seamlessly, I've tried putting that little bit in all sorts of different tags, and even changing the file to a php file instead of an html one. I've verified that php is installed and my path is correct. background.php exists in the same directory as seen here, this and this are the sites I referenced when creating it. The only other info I can think to provide is that I'm viewing it through the live preview in Brackets.

10
  • in simple this is now how things work, more detail on it's way Commented Aug 31, 2015 at 17:48
  • I don't think live preview in Brackets works with PHP. You'll need to run a local webserver, like XAMPP or MAMP. Commented Aug 31, 2015 at 17:50
  • 1
    I see you echoing out a string in echo "$selectedBg" did you mean it to be a variable here? that might be your problem Commented Aug 31, 2015 at 17:51
  • 1
    "and even changing the file to a php file instead of an html one" - well yeah. If you haven't instructed Apache to treat .html files as PHP, you kind of don't have a choice BUT to use a .php extension. Commented Aug 31, 2015 at 17:51
  • 1
    what does show in source code once loaded in your browser ? Commented Aug 31, 2015 at 18:18

1 Answer 1

1

Changing the file to a php file fixed it. The whole file now looks like.

<html>
    <?php include 'background.php';?>
    <title>Yay</title>

    <head>
        <link rel="stylesheet" type="text/css" href="main.css">
        <style type="text/css">
            body {
                background: url(<?php echo $wall;?>);
            }
        </style>
    </head>

    <body>
    </body>
</html>

where background.php is

<?php
  $bg = array('1.png', '2.png', '3.png', '4.png', '5.png', '6.png', '7.png', '8.png');

  $i = rand(0, count($bg)-1);
  $selected = "$bg[$i]";
  $wall = "images/$selected";
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.