0

I would like to derive my website background image from database.those background images are loading through banner.css file.

banner.css 

<?
header("Content-type: text/css");  
include("getfrontimage.php");
$theme = "images";
$size = "cover";
$height = "780px";

?>
<style>
.w3layouts-banner-top{
    background: url(<?php  echo $theme.'/'.$row1[image];?>) no-repeat 0px 0px;
    background-size: <?php echo $size; ?>;
    -webkit-background-size: <?php echo  $size; ?>;
    -moz-background-size: <?php echo $size; ?>;
    -o-background-size: <?php echo $size; ?>;     
    -moz-background-size: <?php echo $size; ?>;
    min-height: <?php echo $height; ?>;
}
.w3layouts-banner-top1{ 
    background: url(<?php echo  $theme.'/'.$row2[image]; ?>) no-repeat 0px 0px;
    background-size: <?php echo $size; ?>;
    -webkit-background-size: <?php echo $size; ?>;
    -moz-background-size: <?php echo $size; ?>;
    -o-background-size: <?php echo $size; ?>;     
    -moz-background-size: <?php echo $size; ?>;
    min-height: <?php echo   $height; ?>;
}
.w3layouts-banner-top2{
    background: url(<?php echo $theme.'/'.$row3[image]; ?>) no-repeat 0px 0px;
    background-size: <?php echo $size; ?>;
    -webkit-background-size: <?php echo $size; ?>;
    -moz-background-size: <?php echo $size; ?>;
    -o-background-size: <?php echo $size; ?>;     
    -moz-background-size: <?php echo $size; ?>;   
    min-height: <?php echo $height; ?>;
}
</style>

But background images are not loading in my website. I refer this links enter link description here

please give me any idea to resolve this problem.

5
  • I would assume you can only do it inside the markup, as php code run there, but not in the css files. Commented May 12, 2018 at 12:16
  • @ZohirSalakCeNa I didn't get sir Commented May 12, 2018 at 12:18
  • Where's your code to initialise variables like $row1, $row2 etc? You may inspect the CSS file from your browser to see if all those variables are set properly. Commented May 12, 2018 at 12:25
  • You might need to rename your file to .php unless otherwise you have set your server to parse the php code in .css file. Commented May 12, 2018 at 12:29
  • @art06 i initialize into include("getfrontimage.php"); file.. and i tried that also Commented May 12, 2018 at 12:35

1 Answer 1

2

$row1[image] must be $row1['image'], because the array key can either be an integer or a string. If you enable display of warnings, you'll get a message in this case: Warning: Use of undefined constant image Correct the other variables.

Processing PHP code in files with the .css extension is not a not quite optimal solution, because you need to add special directives so that your web server processes css files as php files. It is easier to include CSS rules in a php file, and then add the css file to a web page using:

<link rel="stylesheet" href="css/styles.php">

By the way, short tags <?= $size ?> are more readable than <?php echo $size; ?>.

Sign up to request clarification or add additional context in comments.

1 Comment

Did my answer help to solve the problem, @Arebhy Sri?

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.