1

I need my administrator to be able to change/update the banner of my site.

This is the banner code

<div class="containertop">This depends on the background of the div</div>

and this is the CSS for that

.containertop
{
     width:1000px;
     height:300px;  
     **background:url(../images/1.png) no-repeat center;**
     margin: 0 auto;
     margin-top: 40px;
}

What I would like to happen is the same as a Facebook cover photo. When a new banner is uploaded, the CSS will be updated(something like that). But of course, the new banner must be fetched from the database.

So I am thinking that the CSS would become like this:

Fetch the saved banner source and then:

background:url(<?php echo $row['image']; ?>);

but can I do the PHP connection to database (include 'dbname.php') inside a CSS txt?

6
  • 2
    You can dynamically change it using JavaScript or jQuery. Commented Jul 13, 2012 at 14:05
  • 2
    It would be easier to use inline css on the tag itself - move background:url(../images/1.png) no-repeat center; into the tag as a style element, then set that in PHP. No need for JS here. Commented Jul 13, 2012 at 14:06
  • coolphptools.com/dynamic_css check this Commented Jul 13, 2012 at 14:06
  • how can i do that? are there any tutorial links you can recommend? Commented Jul 13, 2012 at 14:06
  • 1
    See my answer here. Commented Jul 13, 2012 at 14:10

4 Answers 4

2

There's nothing preventing you to serve a css generated by PHP. That's even easy.

Simply start your php file like this :

<?php
header("Content-Type: text/css");
Sign up to request clarification or add additional context in comments.

1 Comment

Nice. Doing something like this had never occurred to me before. I need to peruse SO more often (throws in toolbox... wanders off in random direction).
1

I agree with Ben. If you make a little embedded css section in your page, you could put the .containerTop css code there. Then, put your code in the page. So, in your actual web page, put this:

<style type="text/css">
.containertop{
width:1000px;
height:300px;  
background:url(<?php echo $row['image']; ?>);
margin: 0 auto;
margin-top: 40px;
}
</style>

Of course, your background url will not update until it is reloaded. If you decide to do it this way, don't forget to take the .containerTop definition out of your existing css. Having said all that, I really like dystroy's answer. Very nice. I never thought of doing that.

Comments

1

You can set containertop background while loading php file.

<?php
echo "<style>.containertop{ background:url(../images/".$row['image'].") no-repeat center;}</style>"
?>

This will set the background fetched from db.

Comments

0

Well, You can use jQuery to change/overwrite the CSS file.

Example -

$('.containertop').css('backgroud','url(images/change.jpg) repeat');

or

$('.containertop').css('backgroud','url(<?php echo $images_url ?>) repeat');

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.