2

i want to change the color scheme of my webpage...so i have given the color picker and other tools in my website and i store those values of color in a MySql database..

but i cannot figure out how can i apply those styles values from a mysql database to a css or an html file..

for example:

the color value stored by user-1 in the database is :#ffffff and the other value by user-2 is:#000000

how can i get these values to be applied in css every time the user logs-in.. i.e, when user-1 logs-in i want the body background to be #ffffff and for iser-2 i want it to be #000000

2
  • Could you edit your question to tell us a little more about the site and how/why your users will change their styles? Commented May 1, 2013 at 4:42
  • 1
    Should user be able to define any color or is it just a few colors to choose from? Commented May 1, 2013 at 4:54

4 Answers 4

2

There are various ways this can be done. 1. Convert your CSS to PHP, i.e. CSS is generated, but everything can be dynamic - probably overkill for a few colours 2. Overwrite the styles in the stylesheet

The simplest way, probably not the best though is number 2, over write the style after the stylesheet with the new colour.

i.e (sudo code)

<html><head>
....
<style ... src="..." />
<style>
// from db
#custom { background: #<?php echo $colour; ?> }
</style>

etc

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

Comments

1

It is better you use your JavaScript to change the style between PHP tags.

For example:

if($response){

    echo "<script>
        $('#matter_table').css('display', 'block');
    </script>";
}
else{

    echo "<script>
        $('#matter_table').css('display', 'none');
    </script>";
}

Comments

0

I use this trick :

I create a dynamicss.php file and in it use :

$color = '#6b82a2';
$css = <<<HERE

body{
    font-family:Arial, Helvetica, sans-serif;
    background-color : $color ;
    background-image: url("../img/texture.png");
}

HERE;

$bytes = file_put_contents('css/mainCss.css',$css);

you can load your variables and then use in hereDoc.

2 Comments

In general I like the idea behind this approach. But if this appends the css every time a visitor hits a page on your site your css file would get unruly in a hurry. I'd almost rather use your technique but set the content-type to css and call dynamiccss.php as a second stylesheet, loaded after the first.
Thanks for your comment, Yes! it is better
0

You can achieve this by following steps:

  1. Make your website template flexible if user change one style it should reflect on all theme.

  2. When user logged in you can fetch that user theme color from db and you can store it on session or any other possible way.

    e.g : $themecolor = '#000000';
    
  3. Pass that theme color to body background inline style

    e.g : style = "background-color : <?php echo $themecolor; ?>"
    

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.